iOS Autolayout – How to set two different distances between views, depends on the screen height

You can create an NSLayoutConstraint outlet on your view controller and connect the outlet to the activity indicator’s Y constraint in your xib or storyboard. Then, add an updateViewContraints method to your view controller and update the constraint’s constant according to the screen size.

connecting constraint to outlet

Here’s an example of updateViewConstraints:

- (void)updateViewConstraints {
    [super updateViewConstraints];
    self.activityIndicatorYConstraint.constant =
        [UIScreen mainScreen].bounds.size.height > 480.0f ? 200 : 100;
}

Of course you will want to put in your appropriate values instead of 200 and 100. You might want to define some named constants. Also, don’t forget to call [super updateViewConstraints].

Leave a Comment