0

On iOS 9.3 iPhones and iPads (actual devices and in the simulator), I am getting inconsistent information (between the device and the simulator) about the [UIScreen mainScreen].bounds after an orientation change notification.

My view controller adds a notification for orientation changes when the view loads:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(deviceOrientationDidChangeNotification:)
 name:UIDeviceOrientationDidChangeNotification
 object:nil];

The behaviour when the orientation changes is as follows:

- (void) deviceOrientationDidChangeNotification:(NSNotification *) notification {
    CGRect frame = [UIScreen mainScreen].bounds;
    NSLog(@"Main screen is w=%f h=%f", frame.size.width, frame.size.height);
}

Running this on an iPhone, after the orientation changes, the log message indicates a frame width and height that correspond with the post orientation change screen dimensions.

Running this on an iPad, after the orientation changes, the log message indicates a frame width and height that correspond with the pre orientation change screen dimensions.

Is this kind of inconsistency something that needs to be coded around? I could start trying to detect what the orientation is (landscape or portrait) and then using the width and height values that make sense but that is pretty hacky.

Geoff S
  • 175
  • 2
  • 13
  • I have tried using the `UIView` category suggested in [this SO answer](http://stackoverflow.com/questions/26069874/what-is-the-right-way-to-handle-orientation-changes-in-ios-8/27409619#27409619) but, on an iPad, when the screen rotates into a portrait orientation, the device thinks it is in landscape and when the screen rotates into a landscape orientation, the device thinks it is in portrait. – Geoff S Jun 15 '16 at 22:58

1 Answers1

0

The right way to handle this is to avoid the notification system, instead using the - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator

method on the UIViewController where you want to adapt the UI for an orientation change.

Geoff S
  • 175
  • 2
  • 13