How Do I detect the orientation of the device on iOS?
Really old thread, but no real solution.
I Had the same problem, but found out that getting The UIDeviceOrientation isn't always consistent, so instead use this:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;if(orientation == 0) //Default orientation //UI is in Default (Portrait) -- this is really a just a failsafe. else if(orientation == UIInterfaceOrientationPortrait) //Do something if the orientation is in Portraitelse if(orientation == UIInterfaceOrientationLandscapeLeft) // Do something if Leftelse if(orientation == UIInterfaceOrientationLandscapeRight) //Do something if right
if UIViewController:
if (UIDeviceOrientationIsLandscape(self.interfaceOrientation)){ // }
if UIView:
if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)){ //}
UIDevice.h:
#define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
Updated:
add this code to xxx-Prefix.pch then you can use it anywhere:
// check device orientation#define dDeviceOrientation [[UIDevice currentDevice] orientation]#define isPortrait UIDeviceOrientationIsPortrait(dDeviceOrientation)#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)#define isFaceUp dDeviceOrientation == UIDeviceOrientationFaceUp ? YES : NO#define isFaceDown dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
usage:
if (isLandscape) { NSLog(@"Landscape"); }
For what You looking for first you have to Get Notification if Orientation Changed!You Can set This Thing in viewDidLoad like
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
and whenever Orientation of your Device changed OrientationDidChange Called where You can do whatever You Want as Per Orientation
-(void)OrientationDidChange:(NSNotification*)notification{ UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation]; if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight) { } else if(Orientation==UIDeviceOrientationPortrait) { }}