iOS自动布局约束在iOS 6中被打破,但在iOS 7中很好

iOS自动布局约束在iOS 6中被打破,但在iOS 7中很好,ios,uitableview,autolayout,Ios,Uitableview,Autolayout,我目前正在使用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 al

我目前正在使用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>

我不使用这个库,但是快速查看文档和源代码可以使它变得清晰<可以多次调用
UIView
上的code>updateConstraint,并且通常会多次调用。当您查看代码时,您使用的是
mas\u makeConstraints
。换句话说,当调用
updateConstraints
时,您将一次又一次地添加这些约束。这就是重复这些约束的原因。你有两个选择

mas\u makeConstraints
替换为
mas\u updateConstraints
。请阅读以下文档:

添加了-(NSArray*)mas_updateconstraint:(void(^)(MASConstraintMaker*)块,如果可能,它将更新现有约束,否则将添加它们。这使得在UIView-(void)updateConstraints方法中使用砌体变得更容易,这是apple建议添加/更新约束的地方

或者,您可以在块中的
MASConstraintMaker
对象上将
updateExisting
设置为
YES


应该可以解决您的问题。

效果非常神奇!嗯,不完全像魔术,因为你的解释对我来说很清楚。非常感谢;)
- (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);
    }];
}