Current iphone device orientation in ViewDidLoad

Please help me. I have methods:

-(void) getCurrentOrientation{ UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; if(orientation == UIInterfaceOrientationPortrait){ NSLog(@"portrait"); } else if(orientation == UIInterfaceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } else if(orientation == UIInterfaceOrientationLandscapeLeft) { NSLog(@"LandscapeLeft"); } } 

but when i call it getCurrentOrientation throw viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; [self getCurrentOrientation]; } 

NSLog is empty. What's wrong? I also try

 - (void)viewDidLoad { [super viewDidLoad]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) { NSLog(@"portrait"); } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } } 

but this varint is also empty.

I need to know in which ORIENTATION user runs APP!

Please give me some advice.

+8
ios iphone
source share
5 answers

Your code is absolutely correct and there are no problems.

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

This operator only works in original devices.

However, you want to check the simulator, which you can check as

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { // portrait } else { // landscape } 

Update:

I tested you both on the device and on the simulator. Initially, it will only show a portrait in viewDidLoad, although you will keep the device in the landscape. The first controller will consider the shouldAutoRotate method.

You should not depend on viewDidLoad for the initial orientation. You must first depend on the toAutorotate method for precise orientation.

+15
source share

Try replacing your toAutorotateMethod method below shouldAutorotate

  - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationPortrait){ NSLog(@"portrait"); } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { NSLog(@"LandscapeLeft"); } return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) } 

It can help you.

+2
source share

The first time the application is downloaded, UIDevice.current.orientation is invalid. But UIApplication.shared.statusBarOrientation. However, UIDevice.current.orientation is the best way to check orientation, in general. Thus, this method will handle all situations

 func isLandscape() -> Bool { return UIDevice.current.orientation.isValidInterfaceOrientation ? UIDevice.current.orientation.isLandscape : UIApplication.shared.statusBarOrientation.isLandscape } 
+2
source share

If you just want to check the orientation of the application, use the following code:

 - (void) viewDidLoad { [super viewDidLoad]; BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation); // now do whatever you need } 

or

 -(void)viewDidLoad { if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) { //landscape view code } else { //portrait view code } } 
+1
source share

Is your viewDidLoad ? Did you set a breakpoint there?

What about printing NSLog(@"%i", [[UIDevice currentDevice] orientation]) ?

The documentation states that orientation always returns 0 unless you called beginGeneratingDeviceOrientationNotifications . Maybe calling him before trying to get orientation is not enough. Try transferring the call to application:didFinishLaunchingWithOptions:

However, it is best to use the orientation supplied by the controller - [UIViewController interfaceOrientation] , and the parameter passed to shouldAutorotateToInterfaceOrientation .

+1
source share

All Articles