如何使用自动布局使用4英寸故事板修复iPhone屏幕宽度?

时间:2015-06-15 18:16:13

标签: storyboard autolayout width iphone-5

我是新手自动布局。我正在使用iPhone 4英寸故事板。在视图容器中,我有4个按钮。它在iPhone 5中运行良好,但是当它在iPhone 6和6+中运行时,右侧有一些空间。视图大小使用以下代码修复,但不是4个按钮。

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGRect bounds = self.view.bounds;
    self.contentView.frame = bounds;
}                                     

1 个答案:

答案 0 :(得分:1)

- (void) viewDidLayoutSubviews {
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *bindings = NSDictionaryOfVariableBindings(self.view);
    NSString *formatTemplate = @"%@:|[self.view]|";
    for (NSString * axis in @[@"H",@"V"]) {
        NSString * format = [NSString stringWithFormat:formatTemplate,axis];
        NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:bindings];
        [self.view.superview addConstraints:constraints];
    }

}

I getting reference from here.