Detecting iOS UIDevice orientation Detecting iOS UIDevice orientation ios ios

Detecting iOS UIDevice orientation


Try doing the following when the application loads or when your view loads:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];[[NSNotificationCenter defaultCenter]   addObserver:self selector:@selector(orientationChanged:)   name:UIDeviceOrientationDidChangeNotification   object:[UIDevice currentDevice]];

Then add the following method:

- (void) orientationChanged:(NSNotification *)note{   UIDevice * device = note.object;   switch(device.orientation)   {       case UIDeviceOrientationPortrait:       /* start special animation */       break;       case UIDeviceOrientationPortraitUpsideDown:       /* start special animation */       break;       default:       break;   };}

The above will allow you to register for orientation changes of the device without enabling the autorotate of your view.


Note

In all cases in iOS, when you add an observor, also remove it at appropriate times (possibly, but not always, when the view appears/disappears). You can only have "pairs" of observe/unobserve code. If you do not do this the app will crash. Choosing where to observe/unobserve is beyond the scope of this QA. However you must have an "unobserve" to match the "observe" code above.


If you came to this question looking for how to detect an orientation change (without necessarily wanting to disable the rotation), you should also be aware of viewWillTransitionToSize, which is available from iOS 8.

Swift example from here

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in        let orient = UIApplication.sharedApplication().statusBarOrientation        switch orient {        case .Portrait:            println("Portrait")            // Do something        default:            println("Anything But Portrait")            // Do something else        }        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in            println("rotation completed")    })    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)}

And if you don't need to worry about the actual orientation:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {    // do something    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)}

Objective-C example from here

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{       [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)    {        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];        // do whatever    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)    {     }];    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];}

And if you don't need to worry about the actual orientation (taken from this answer):

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{    // Do view manipulation here.    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];}

See also


1) Swift version of David's answer2) In case you still want to detect orientation when there's no orientation change (Swift vesion of Moe's answer to How Do I detect the orientation of the device on iOS?)

    // Initial device orientation    let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation    if(orientation == UIInterfaceOrientation.Unknown){        // code for Unknown    }    else if(orientation == UIInterfaceOrientation.Portrait){        // code for Portrait    }    else if(orientation == UIInterfaceOrientation.PortraitUpsideDown){        // code for Portrait    }    else if(orientation == UIInterfaceOrientation.LandscapeRight){        // code for Landscape            }    else if(orientation == UIInterfaceOrientation.LandscapeLeft){        // ode for Landscape    }    // To detect device orientation change    UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()    NSNotificationCenter.defaultCenter().addObserver(        self,        selector: "orientationChanged:",        name: UIDeviceOrientationDidChangeNotification,        object: UIDevice.currentDevice())

orientationChanged function

func orientationChanged(note: NSNotification){    let device: UIDevice = note.object as! UIDevice    switch(device.orientation)    {        case UIDeviceOrientation.Portrait:        // code for Portrait        break        case UIDeviceOrientation.PortraitUpsideDown:        // code for Portrait        break        case UIDeviceOrientation.LandscapeLeft:        // code for Landscape        break        case UIDeviceOrientation.LandscapeRight:        // code for Landscape        break        case UIDeviceOrientation.Unknown:        // code for Unknown        break        default:        break    }}