0

我正在尝试在视图底部上方添加一个按钮。我正在以编程方式添加自动布局约束,这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"height : %f", self.view.bounds.size.height);
    [self.view addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=topSpace)-[button1]"
                                             options:0
                                             metrics:@{@"topSpace":@(self.view.bounds.size.height*0.9f)}
                                               views:@{@"button1" : self.button1}]];

    // Do any additional setup after loading the view, typically from a nib.
}

此代码的结果显示在图像在此处输入图像描述中,按钮肯定不在超级视图的底部。当我运行我的程序时,我也会收到以下警告。

2014-06-01 22:02:19.144 AutoLayout[2886:60b] 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:0x8d40590 'IB auto generated at build time for view with fixed frame' V:|-(269)-[UIButton:0x8d815c0]   (Names: '|':UIView:0x8d82360 )>",
    "<NSLayoutConstraint:0x8c34f60 V:|-(>=432)-[UIButton:0x8d815c0]   (Names: '|':UIView:0x8d82360 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8c34f60 V:|-(>=432)-[UIButton:0x8d815c0]   (Names: '|':UIView:0x8d82360 )>

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.

关于这两个问题的任何帮助。

4

1 回答 1

0

一个 ">=" 约束本身不足以定位视图(系统如何知道更大多少?)。请改用“=”约束。如果您自己不添加任何约束,则 NSIBPrototypingLayoutConstraint 是系统在 IB 中添加的约束。如果您要在 IB 中添加按钮,那么在那里添加约束会更容易。如果您想在代码中添加约束,那么也可以在代码中创建并添加您的按钮(另一种方法是使用 IB 添加的按钮,在添加您自己的之前删除那里所做的任何约束)。

于 2014-06-01T19:34:15.373 回答