2013-10-15 159 views
0

我插入一個按鈕,一個視圖中的故事板類似如下:enter image description hereIOS添加按鈕自動佈局

,我需要添加自定義使用下面的代碼限制到底部:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSDictionary *viewsDictionary = [NSDictionary dictionaryWithObject:self.bottomButton forKey:@"bottomButton"]; 
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[bottomButton]-90-|" options:0 metrics:nil views:viewsDictionary]; 
    NSArray *v_constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomButton]-20-|" options:0 metrics:nil views:viewsDictionary]; 
    [self.bottomButton.superview addConstraints:constraints]; 
    [self.bottomButton.superview addConstraints:v_constraints]; 
} 

,但有一個衝突,錯誤是

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) 
(
    "<NSIBPrototypingLayoutConstraint:0x8c57fb0 'IB auto generated at build time for view with fixed frame' H:|-(254)-[UIButton:0x8c56440](LTR) (Names: '|':UIView:0x8c56af0)>", 
    "<NSIBPrototypingLayoutConstraint:0x8c58de0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x8c56440(46)]>", 
    "<NSLayoutConstraint:0x8c4fc30 H:[UIButton:0x8c56440]-(90)-| (Names: '|':UIView:0x8c56af0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x8c62320 h=--& v=--& H:[UIView:0x8c56af0(320)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8c4fc30 H:[UIButton:0x8c56440]-(90)-| (Names: '|':UIView:0x8c56af0)> 

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. 
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) 
(
    "<NSIBPrototypingLayoutConstraint:0x8c58d30 'IB auto generated at build time for view with fixed frame' V:|-(361)-[UIButton:0x8c56440] (Names: '|':UIView:0x8c56af0)>", 
    "<NSIBPrototypingLayoutConstraint:0x8c58e10 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x8c56440(30)]>", 
    "<NSLayoutConstraint:0x8c527c0 V:[UIButton:0x8c56440]-(20)-| (Names: '|':UIView:0x8c56af0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x8c62380 h=--& v=--& V:[UIView:0x8c56af0(568)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8c527c0 V:[UIButton:0x8c56440]-(20)-| (Names: '|':UIView:0x8c56af0)> 

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. 

如何解決?

+0

在故事板本身設置約束條件,這很容易。 –

回答

3

發生這種情況的原因是,即使您沒有在IB中明確設置約束,他們也會在運行時自動添加,例如,

"<NSIBPrototypingLayoutConstraint:0x8c57fb0 'IB auto generated at build time for view with fixed frame' H:|-(254)-[UIButton:0x8c56440](LTR) (Names: '|':UIView:0x8c56af0)>", 

請從2013年WWDC觀看會話406目前最好的做法是設置約束IB,然後只修改/中不能使用IB設置代碼添加約束。