2013-03-12 72 views
16

我有一个labelbutton在这样一个superView增加布局约束的优先级

|--------------[Label]-----[button]-| 

我想要labelcentred如果可能的话,那么有差距最小的button并移动到左边。

因此,如果按钮是大的,它看起来像......

|-[  LABEL!  ]-[button]-| 

所以,按钮停留的地方是,在相同的尺寸。元素之间有最小的差距。

我可以添加centerX约束,但我似乎无法给它一个优先级,所以它仍然Required

我该如何创建这种情况?我正在做代码中的所有自动布局。

我现在有是约束...

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-(>[email protected])-[_button(==45)]-|" 
                  options:NSLayoutFormatAlignAllCenterY 
                  metrics:nil 
                   views:views]]; 

[self addConstraint:[NSLayoutConstraint constraintWithItem:_label 
               attribute:NSLayoutAttributeCenterX 
               relatedBy:NSLayoutRelationEqual 
                toItem:self.contentView 
               attribute:NSLayoutAttributeCenterX 
               multiplier:1.0 
                constant:0.0]]; 

但我不知道如何降低第二约束的优先级。

回答

35

你刚才设置的约束priority财产,像这样:

NSLayoutConstraint *centeringConstraint = 
    [NSLayoutConstraint constraintWithItem:_label 
           attribute:NSLayoutAttributeCenterX 
           relatedBy:NSLayoutRelationEqual 
            toItem:self.contentView 
           attribute:NSLayoutAttributeCenterX 
           multiplier:1.0 
            constant:0.0]; 

centeringConstraint.priority = 800; // <-- this line 

[self addConstraint:centeringConstraint]; 
+0

当然!谢谢!我没想过在添加它之前创建对象。完善!现在排序,谢谢! – Fogmeister 2013-03-12 15:35:41