如何避免使用程序化自动布局进行硬编码

时间:2015-08-21 15:31:54

标签: ios objective-c user-interface autolayout interface-builder

我在使用autolayout时遇到了一个问题(我是新手),尽管我的约束功能正常(水平居中,我想要的是垂直间距),当我向横向移动时,底部按钮消失

我知道发生这种情况是因为我基于纵向方向视图约束了我的对象,当我们移动到横向时,当高度和宽度值移动时,这不再适用。在改变方向时,我真的不知道如何解释这些变化。有什么建议吗?

下面的代码和截图:

simulator capture

-(void)setConstraints {
    [self.view removeConstraints:self.view.constraints];

    UIButton *cameraButton = self.cameraButton;
    UILabel *camera = self.videoLabel;
    UIButton *libraryButton = self.libraryButton;
    UILabel *library = self.libraryLabel;


    NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);

    NSDictionary *metrics = @{@"horizontalSpacing":@500.0, @"verticalSpacing":@125};


    //set up top button to be horizontally centered
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];

    //set up top button vertical from top of superview
    constraints = [constraints arrayByAddingObjectsFromArray:
                   [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
                                                           options:0
                                                           metrics:nil
                                                             views:views]];
    //set up top button label to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:
                   [NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
                                                           options:0
                                                           metrics:nil
                                                             views:views]];

    //set up second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:
                   [NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
                                                           options:0
                                                           metrics:nil
                                                             views:views]];
    //set up label for second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:
                   [NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
                                                           options:0
                                                           metrics:nil
                                                             views:views]];

    //set up vertical constraints by spacing ALL objects appropriately
    constraints = [constraints arrayByAddingObjectsFromArray:
                   [NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
                                                           options:0
                                                           metrics:metrics
                                                             views:views]];


    self.libraryLabel.textAlignment = NSTextAlignmentCenter;
    self.videoLabel.textAlignment = NSTextAlignmentCenter;

    self.libraryLabel.backgroundColor = [UIColor redColor];

    [self.view addConstraints:constraints];

}

1 个答案:

答案 0 :(得分:0)

你得到了你所要求的。如果那不是你想要的,那就不要求了!

考虑你的垂直约束:

@"V:|-175-[cameraButton]"
@"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"

从上到下构成一系列约束 。当然,如果屏幕更短而不是175加上verticalSpacing加上其他视图的尺寸和最小间距,则底部视图将偏离屏幕底部。

如果那不是您想要的,请更改您的设计,使其不是您所获得的。例如,从上到下放置一些视图,从下到上放置一些视图。或者允许你的一些空格改变为超视图高度的百分比。