繁体   English   中英

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

[英]iOS auto-layout constraints broken in iOS 6, but fine in iOS7

我目前正在使用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);
    }];
}

我不使用此库,但快速浏览文档和源代码可以使它整洁。 UIView上的updateConstraints可以多次调用,通常也可以多次调用。 在查看代码时,您正在使用mas_makeConstraints 换句话说,当调用updateConstraints时,您一次又一次地添加这些约束。 这就是这些约束条件重复的原因。 您有两种选择...

mas_makeConstraints替换为mas_updateConstraints 从docs阅读:

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

或者,你可以设置updateExistingYES您的MASConstraintMaker在你的块状物。

应该解决你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM