ios添加按钮自动布局

Posted

技术标签:

【中文标题】ios添加按钮自动布局【英文标题】:ios add button autolayout 【发布时间】:2013-10-15 08:12:55 【问题描述】:

我在情节提要的视图中插入一个按钮,如下所示:

我需要使用以下代码在底部添加自定义约束:

- (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.

如何解决?

【问题讨论】:

在storyboard自己设置约束,很简单。 【参考方案1】:

这是因为即使您没有在 IB 中明确设置约束,它们也会在运行时自动添加,例如

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

请观看 WWDC 2013 的第 406 节。当前的最佳做法是在 IB 中设置约束,然后仅在无法使用 IB 设置的代码中修改/添加约束。

【讨论】:

以上是关于ios添加按钮自动布局的主要内容,如果未能解决你的问题,请参考以下文章

自动布局是拉伸图像,自动布局,ios

如何在 iOS7 中使用自动布局让按钮垂直增长?

自动布局在 ios 8.3 上无法正常工作

iOS 自定义键盘自动布局

约束/自动布局栏隐藏,Xcode 6

iOS - 按钮的自动布局/大小类