iOS学霸笔记程序员iOS开发

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

2016-03-31  本文已影响6782人  苹果API搬运工

相信大家在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.

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


   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];
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;


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

上一篇 下一篇

猜你喜欢

热点阅读