Search code examples
iosuiwebvieworientationscreen-orientationorientation-changes

iOS: How do I figure out the current orientation in viewWillAppear?


I have a UIWebView that I would like to load different URLs depending on whether its portrait or landscape. How can I figure out what my current orientation is inside viewWillAppear?


Solution

  • Use UIApplication's statusBarOrientation property. You may run into problems if you use the device orientation if it is UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown.

    Example

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
       // Do something when in landscape
    }
    else
    {
       // Do something when in portrait
    }