iOS 6中的iOS自动布局约束已经破坏,但在iOS7中却很好

时间:2014-03-13 05:52:18

标签: ios uitableview autolayout

我目前正在使用Masonry DSL设置iOS自动布局的UITableViewCell。它在iOS 7中运行良好,但在iOS 6中,它大声喊出一堆破坏的约束警告:

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-03-13 14:41:07.422 clear[754:907] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<MASLayoutConstraint:0x1ed1e180 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY>",
    "<MASLayoutConstraint:0x1ed00f50 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x1ed1e180 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-03-13 14:41:07.451 clear[754:907] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<MASLayoutConstraint:0x1edf0010 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75>",
    "<MASLayoutConstraint:0x1ed309f0 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x1edf0010 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75>

我注意到的一件事是某些约束(如上所示)是重复的,因此iOS必须删除其中一个。下面附有代码片段。任何人都可以帮我找出问题所在并摆脱这条警告信息?

UITableViewCell updateConstraints

- (void)updateConstraints
{
    [super updateConstraints];

    [self.coverImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(20);
        make.top.equalTo(self.contentView).with.offset(5);
        make.bottom.equalTo(self.contentView).with.offset(-5);

        make.width.equalTo(self.coverImage.mas_height).multipliedBy(0.75);
    }];

    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverImage.mas_right).with.offset(10);
        make.centerY.equalTo(self.contentView).priorityLow();
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView);
        make.top.equalTo(self.containerView);
    }];

    [self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(5);
        make.left.equalTo(self.containerView);
        make.bottom.equalTo(self.containerView);

        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];

    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatarImage.mas_right).with.offset(5);
        make.centerY.equalTo(self.avatarImage);
    }];

    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).with.offset(-10);
        make.top.equalTo(self.contentView).with.offset(5);
    }];
}

1 个答案:

答案 0 :(得分:1)

我没有使用这个库,但快速查看文档和源代码可以使它干净。您updateConstraints上的UIView可以被多次调用,通常不止一次被调用。当您查看代码时,您正在使用mas_makeConstraints。换句话说,当调用updateConstraints时,您会一次又一次地再次添加这些约束。这就是为什么这些约束重复的原因。你有两个选择......

mas_makeConstraints替换为mas_updateConstraints。请从文档中阅读:

  

添加 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))块,如果可能,将更新现有约束,否则将添加它们。这样可以更容易地在UIView - (void)updateConstraints方法中使用Masonry,这是用于通过apple添加/更新约束的推荐位置。

或者,您可以将<{1}}设置为您updateExisting对象中的YES

应该解决你的问题。