如何垂直居中两个视图&相对于超视图,宽度是否相等?

时间:2015-06-10 11:35:23

标签: objective-c uiview uibutton nslayoutconstraint

我正在尝试将Button中的两个View对齐(以青色显示)。它们在View中垂直对齐。当我尝试水平对齐它们时,它们的高度会发生变化。我希望它们的宽度应该与超级视图的宽度相同,而不像较小的Next按钮。只能使用VFL

来实现这一点

2 Buttons to in a container view

以下是代码:

[self prepareButton:saveButton label:@"SAVE"];
[buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:saveButton
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:buttonContainerView
                                                              attribute:NSLayoutAttributeCenterY
                                                             multiplier:1.00f
                                                               constant:0]];
[saveButton layoutIfNeeded];

[self prepareButton:nextButton label:@"NEXT"];
[buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:nextButton
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:buttonContainerView
                                                              attribute:NSLayoutAttributeCenterY
                                                             multiplier:1.00f
                                                               constant:0]];
[nextButton layoutIfNeeded];
[buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saveButton]-[nextButton]-|"
                                                                          options:0
                                                                          metrics:metrics
                                                                            views:viewDictionary]];
[saveButton layoutIfNeeded];
[nextButton layoutIfNeeded];

-(void)prepareButton:(UIButton *)button
           label:(NSString *) label
{
    [buttonContainerView addSubview:button];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    button.backgroundColor = [UIColor redColor];
    button.layer.cornerRadius = 2.50f;
    [button setTitle:label forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

1 个答案:

答案 0 :(得分:0)

通过设置相同的高度和放大来解决问题在VFL中分配优先级:

[buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saveButton]-[nextButton(==saveButton@1000)]-|"
                                                                          options:0
                                                                          metrics:metrics
                                                                            views:viewDictionary]];

Equal sized buttons in a container view