27

Possible Duplicate:
iPhone orientation

How to check the orientation of device at any point off time programmatically in iPhone ?

Thank You.

Community
  • 1
  • 1
suse
  • 10,503
  • 23
  • 79
  • 113
  • 1
    Possible duplicated [iPhone orientation](http://stackoverflow.com/q/634745/194544) – beryllium Feb 20 '12 at 15:41
  • Try this : http://jayprakashdubey.blogspot.in/2014/07/check-device-orientation.html – Jayprakash Dubey Jul 30 '14 at 10:38
  • You could use a method like the willTransitionToTraitCollection:withTransitionCoordinator: This method is called any time the trait collection of the view controller changes. Traits can be the horizontal and vertical size classes. So inside the method we can check the following (in Swift):switch newCollection.verticalSizeClass { case .Compact: //it's landscape case .Regular, .Unspecified: //It's portrait } – angelos.p Feb 10 '16 at 20:08

6 Answers6

74

Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait

so rather checking separately you can do it like this ...

   if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }

OR

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }
Azhar
  • 20,500
  • 38
  • 146
  • 211
17

Try it.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

if ( ([[UIDevice currentDevice] orientation] ==  UIDeviceOrientationPortrait)  )
{
   // do something for Portrait mode
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
   // do something for Landscape mode
}
caulitomaz
  • 2,141
  • 14
  • 20
Sanket Pandya
  • 1,095
  • 7
  • 21
9

Try also:

UIInterfaceOrientationIsPortrait(orientation)

and

UIInterfaceOrientationIsLandscape(orientation)
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
7

-[UIViewController interfaceOrientation]

However it is possible to retrieve the deviceOrientation, independent from the current interface orientation:

[[UIDevice currentDevice] orientation]

Read the documentation for more information though, you need to do some more things to make that work.

Joost
  • 10,333
  • 4
  • 55
  • 61
4

You can also check within the function

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       //do for portrait
    }       
    else 
    { 
       //do for Landscape 
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
kumar123
  • 791
  • 7
  • 21
3

You should place [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; in your AppDelegate didFinishLaunchingWithOptions

Then, anywhere in your application you can get the current orientation with:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

And test orientation with:

UIInterfaceOrientationIsPortrait(orientation) UIInterfaceOrientationIsLandscape(orientation)

Kyle
  • 434
  • 2
  • 10
  • You should test the variable `orientation` using `UIDeviceOrientationIsLandscape` not using `UIInterfaceOrientationIsLandscape`. – Webdevotion May 04 '15 at 19:16