自动布局不工作和警告

问题描述:

我想补充一点,我认为底部上方的按钮。我编程方式添加自动版式的限制,这里是我的code。

I am trying to add a button just above the bottom of my view. I am adding the autolayout constraints programatically, and here is my code.

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

这code的结果显示在图像中按钮肯定是不是在上海华的底部。另外,我得到以下警告,当我运行我的程序。

The result of this code is shown in the image the button is certainly not in the bottom of the superview. Also i am getting the following warnings when i run my program.

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.

在这两个问题上的任何帮助。

Any help on these two issues.

A> =本身的约束是不够的,位置的图(系统怎样知道有更大的?)。请使用=,而不是约束。该NSIBPrototypingLayoutConstraint是由系统中加入IB如果不加任何约束自己。如果您要添加在IB的按钮,那么这将是更容易添加的约束有作为。如果你想添加约束code,然后创建并添加您在code按钮以及(替代,使用IB添加的按钮,是添加您自己之前删除作出那里,任何约束)。

A ">=" constraint by itself is not enough to position a view (how does the system know how much greater?). Use an "=" constraint instead. The NSIBPrototypingLayoutConstraint are constraints added by the system in IB if you don't add any yourself. If you're going to add the button in IB, then it would be easier to add the constraints there as well. If you want to add constraints in code, then create and add your button in code as well (the alternative, using an IB added button, is to remove any constraints made there, before adding your own).