0

I want to know the device orientation when user start my app , in order to produce different view. What I find strange is as below:

- (void)viewDidLoad
{
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        NSLog(@"1");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        NSLog(@"2");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"3");
    }
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"4");
    }
}

No one is printed! I did it on the ipad simulator and I think the orientation should be UIDeviceOrientationPortrait. Why this happen? and how to know the orientation correctly?

itenyh
  • 1,921
  • 2
  • 23
  • 38

5 Answers5

1

Try to use

[UIApplication sharedApplication].statusBarOrientation

instead of

[UIDevice currentDevice].orientation
Mehdzor
  • 789
  • 4
  • 9
  • 23
0

It may be the case that the simulator simply cannot be in an orientation state (it makes no sense, as you can't really rotate your computer...) Have you checked whether it returns UIDeviceOrientationUnknown?

0

The documentation or UIDevice states:

You get the current orientation using the orientation property or receive change notifications by registering for the UIDeviceOrientationDidChangeNotification notification. Before using either of these techniques to get orientation data, you must enable data delivery using the beginGeneratingDeviceOrientationNotifications method

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

So it is probably that you get Unkown orientation because you never started the orientation notifications generation.

You should also log the value in your viewDidLoad to confirm exactly what you receive when you get the orientation. That would be a good starting point for further investigation.

J_D
  • 3,526
  • 20
  • 31
0
Use this :-

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {      
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)     
{       
 //code for portrait   

 }            
else      
{         //code for Landscape 

  }     
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation is the delegate method oa uiviewcontroller.
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
0

According to the docs, the orientation property of UIDevice will always return 0 (i.e., UIDeviceOrientationUnknown) unless -beginGeneratingDeviceOrientationNotifications has been called first. This method will enable the accelerometer, deliver notification changes, and update the orientation property of the UIDevice singleton.

Sean
  • 5,810
  • 2
  • 33
  • 41