不明原因的約束報(bào)錯(cuò)的兩種處理方式

相信大家在iOS的開發(fā)中,經(jīng)常會(huì)遇到一些不明原因的約束警告,有時(shí)候按百度到的方法試一下就好了,有時(shí)候卻不行.而且下一次可能還會(huì)出現(xiàn),比如下面這種約束的報(bào)錯(cuò)

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.

一般來(lái)說(shuō),出現(xiàn)這種警告有兩種情況:

  • 使用xib中自帶的view,對(duì)其中子控件添加了Autolayout約束,在調(diào)用xib后又調(diào)整了view的frame
  • 純代碼方式使用Autolayout,但沒(méi)有對(duì)使用的View的translatesAutoresizingMaskIntoConstraints的屬性設(shè)置為NO.

  • 如果是從代碼層面開始使用Autolayout,需要對(duì)使用的View的translatesAutoresizingMaskIntoConstraints的屬性設(shè)置為NO.
    即可開始通過(guò)代碼添加Constraint,否則View還是會(huì)按照以往的autoresizingMask進(jìn)行計(jì)算.
    而在Interface Builder中默認(rèn)勾選了Ues Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints屬性都會(huì)被默認(rèn)設(shè)置NO.
   UIView *redView = [[UIView alloc] init];
    redView.backgroundColor= [UIColorredColor];

    //禁止autoresizing自動(dòng)轉(zhuǎn)換為約束(特別重要)

    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];
  • 另一種是通過(guò)xib的方式也有可能會(huì)出現(xiàn)這個(gè)警告,這是因?yàn)関iew的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值默認(rèn)就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth

1.從xib里面創(chuàng)建出來(lái)的默認(rèn)控件(Xcode自動(dòng)創(chuàng)建出來(lái)的那個(gè)view)
2.控制器的view

如果不希望控件擁有autoresizingMask的自動(dòng)伸縮功能,應(yīng)該設(shè)置為none.如果從xib中加載自帶的view出現(xiàn)約束警告,也應(yīng)該將其設(shè)置為none

blueView.autoresizingMask = UIViewAutoresizingNone;


這樣煩人的約束警告就不會(huì)再出現(xiàn)了,關(guān)于這兩個(gè)屬性的詳細(xì)解釋,可以查看下面兩個(gè)鏈接中的兩篇文章.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容