iOS Auto-Layout约束在iOS 6中破裂,但在iOS7中很好



我当前使用ios auto-layout使用Masonry DSL设置了我的UITATIONVIEWCELL。它在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必须删除其中之一。附加的代码段。任何人都可以帮助我找出问题所在,并摆脱此警告消息?

uitaiteViewCell 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);
    }];
}

我不使用此lib,但是在文档和源代码中快速查看使其清洁。UIView上的updateConstraints可以多次调用,通常被调用不止一次。当您查看代码时,您正在使用mas_makeConstraints。换句话说,当调用updateConstraints时,您会一次又一次地添加这些约束。这就是这些约束重复的原因。您有两个选择...

mas_updateConstraints替换mas_makeConstraints。从文档中阅读此信息:

添加 - (nsarray *)mas_updateconstraints :( void(^)(masconstraintmaker *))块,该块将在可能的情况下更新现有约束,否则会添加它们。这使得在Uiview - (void)UpdateConstraints方法中更容易使用砖石,这是Apple添加/更新约束的推荐位置。

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

应该解决您的问题。

相关内容

最新更新