Search code examples
storyboardautolayoutwidthiphone-5

How to fix iPhone screen width using 4 inch storyboard using auto layout?


I am new to auto layout. I am using iPhone 4 inch storyboard. Within a view container I have 4 buttons. It is working good in iPhone 5 but when it is running in iPhone 6 and 6+ there is some space at right side. View size is fixed using following code but not 4 buttons.

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

Solution

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