Using top and bottom layout guides to create constraints would probably be the correct solution, but they are available only in Storyboard mode.
In case they are not available (or don't work for some reason), solution is the following:
- Create constraint relative to superview top (actual value doesn't matter at this point, we will set it in code).
- Create outlet for constraint from step 1 (the same way you would for a view).
- Modify your view controller code:
- (void)viewDidLoad {
//initial setup
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
[self applyTopBarOffsetForOrientation:currentOrientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self applyTopBarOffsetForOrientation:toInterfaceOrientation];
}
- (void)applyTopBarOffsetForOrientation:(UIInterfaceOrientation) orientation {
BOOL isPhone = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone;
self.topBarSpacingConstraint.constant = UIDeviceOrientationIsLandscape(orientation) && isPhone ? 52 : 64;
}