美文网首页
IOS 使用Masonry+UITableViewCell布局时

IOS 使用Masonry+UITableViewCell布局时

作者: 透支未来 | 来源:发表于2017-10-19 22:06 被阅读520次

    代码适配,Masonry一般是首选的。项目中tableview使用最多,那么肯定少不了自定义的cell和view,这就牵涉到控件的布局。

    使用过Masonry的都知道,有的时候控制台会输出很多警告,但是布局是正常的,这是为什么呢?

    
    [LayoutConstraints] 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. 
    (
        <MASLayoutConstraint:0x6040004a8d60 ShopLabel:0x7fafc74af500.right == UITableViewCellContentView:0x7fafc7601430.right - 10>,
        <MASLayoutConstraint:0x6040004b6440 ShopLabel:0x7fafc74af500.width == 80>,
        <MASLayoutConstraint:0x6040004b72e0 ShopLabel:0x7fafc76c4e00.left == UITableViewCellContentView:0x7fafc7601430.left + 10>,
        <MASLayoutConstraint:0x6040004b7d60 ShopLabel:0x7fafc76c4e00.right == ShopLabel:0x7fafc74af500.left - 10>,
        <MASLayoutConstraint:0x6040006a7a40 ShopLabel:0x7fafc74b27e0.left == UITableViewCellContentView:0x7fafc7601430.left + 10>,
        <MASLayoutConstraint:0x6040006a6ea0 ShopLabel:0x7fafc74b27e0.width == 53.5705>,
        <MASLayoutConstraint:0x6040006a6180 ShopLabel:0x7fafc74b2ac0.left == ShopLabel:0x7fafc74b27e0.right>,
        <MASLayoutConstraint:0x6040006a66c0 ShopLabel:0x7fafc74b2ac0.right == UITableViewCellContentView:0x7fafc7601430.right - 10>,
        <MASLayoutConstraint:0x6000006ab1c0 ShopLabel:0x7fafc74b2ac0.left == ShopLabel:0x7fafc74b2ac0.right>,
    )
    
    Will attempt to recover by breaking constraint 
    <MASLayoutConstraint:0x6000006ab1c0 ShopLabel:0x7fafc74b2ac0.left == ShopLabel:0x7fafc74b2ac0.right>
    
    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
    
    

    从输出的信息可以知道,有的控件的约束明显重复了设置,所以指出了是哪个控件,重复设置了哪些约束等等。。。。

    Masonry可以设置约束的优先级,优先级分为priorityHigh,priorityMedium,priorityLow(高,中等,低)三个等级。优先级默认为中等,所以当我们对某一个控件的约束条件重复后,会打印警告信息,告诉我们应该去修复它们。

    既然知道了警告的产生原因,那么解决办法有两种:
    1.找到该控件,修改它的相关约束,以消除警告信息。
    2.将控件的约束优先级置为高级,那么就算约束重复了也不会有警告。这也是最简单省事的办法。

    处理方法:

    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).offset(0);
            make.left.mas_equalTo(self.contentView.mas_left).offset(25);
            make.right.mas_equalTo(self.contentView.mas_right).offset(0);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0).priorityHigh(1000);
            make.height.equalTo(@(25));
        }];
    

    相关文章

      网友评论

          本文标题:IOS 使用Masonry+UITableViewCell布局时

          本文链接:https://www.haomeiwen.com/subject/vugpuxtx.html