美文网首页iOS学霸笔记程序员iOS开发
不明原因的约束报错的两种处理方式

不明原因的约束报错的两种处理方式

作者: 苹果API搬运工 | 来源:发表于2016-03-31 10:29 被阅读6782次

    相信大家在iOS的开发中,经常会遇到一些不明原因的约束警告,有时候按百度到的方法试一下就好了,有时候却不行.而且下一次可能还会出现,比如下面这种约束的报错

    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) 
    (
     "<NSLayoutConstraint:0x11d748d0 V:[UITextView:0xc1bb000(65)]>",
     "<NSLayoutConstraint:0x11d77620 V:[UIButton:0x11d71cf0(44)]>",
     "<NSLayoutConstraint:0x11d7be30 V:[UIButton:0x11d79e70(43)]>",
     "<NSLayoutConstraint:0xa1980d0 V:|-(134)-[UITextView:0xc1bb000] (Names: '|':UIView:0xa1afba0 )>",
     "<NSLayoutConstraint:0xa199ed0 UITextView:0xc1bb000.centerY == UIButton:0x11d71cf0.centerY>",
     "<NSLayoutConstraint:0xa199e50 V:[UIButton:0x11d79e70]-(61)-[UIButton:0x11d71cf0]>",
     "<NSLayoutConstraint:0xa199cb0 V:|-(40)-[UIButton:0x11d79e70] (Names: '|':UIView:0xa1afba0 )>"
    )
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x11d748d0 V:[UITextView:0xc1bb000(65)]>
    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.
    

    一般来说,出现这种警告有两种情况:

    • 使用xib中自带的view,对其中子控件添加了Autolayout约束,在调用xib后又调整了view的frame
    • 纯代码方式使用Autolayout,但没有对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO.

    • 如果是从代码层面开始使用Autolayout,需要对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO.
      即可开始通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算.
      而在Interface Builder中默认勾选了Ues Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints属性都会被默认设置NO.
       UIView *redView = [[UIView alloc] init];
        redView.backgroundColor= [UIColorredColor];
    
        //禁止autoresizing自动转换为约束(特别重要)
    
        redView.translatesAutoresizingMaskIntoConstraints= NO;
    
        [self.view addSubview:redView];
    
       
        //添加约束
    
        //高度约束
    
        NSLayoutConstraint*hlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:0.0constant:100];
    
        [redViewaddConstraint:hlc];
    
        //宽度约束
    
        NSLayoutConstraint*wlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:0.0constant:100];
    
        [redViewaddConstraint:wlc];
    
    
        //右边间距约束
    
        NSLayoutConstraint*rightlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeRightmultiplier:1.0constant:-20];
    
        [self.view addConstraint:rightlc];
    
        //底部间距约束
    
        NSLayoutConstraint*bottomlc = [NSLayoutConstraintconstraintWithItem:redView attribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeBottommultiplier:1.0constant:-20];
    
        [self.view addConstraint:bottomlc];
    
    • 另一种是通过xib的方式也有可能会出现这个警告,这是因为view的autoresizingMask属性引起的
    typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    };
    

    一般情况下,以下这些view的autoresizingMask值默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth

    1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view)
    2.控制器的view

    如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none.如果从xib中加载自带的view出现约束警告,也应该将其设置为none

    blueView.autoresizingMask = UIViewAutoresizingNone;


    这样烦人的约束警告就不会再出现了,关于这两个属性的详细解释,可以查看下面两个链接中的两篇文章.

    相关文章

      网友评论

      • vicki753:可是控件的确有伸缩功能,而且还需要,可是它干嘛还出现这个警告,能不要它么
        iDeveloper:我也觉得,苹果这么蛋疼
      • Bug猫:留着,以后以后可能会用到

      本文标题:不明原因的约束报错的两种处理方式

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