[UIDevice currentDevice] .orientation == UIDeviceOrientationUnknown after UINavigationController push

This observation is more than anything else, because I seem to have found a workaround ...

The code below does not work when it is on a controller that has been ported to the UINavigationController stack. In this situation, [UIDevice currentDevice].orientation successively returns UIDeviceOrientationUnknown .

 -(void)layoutAccordingToOrientation { UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; NSLog(@"layoutAccordingToOrientation/orientation==%i", orientation); if (UIInterfaceOrientationIsLandscape(orientation)) { : _detailToolbar.frame = CGRectMake(0, 660, 1024, 44); } else { : _detailToolbar.frame = CGRectMake(0, 916, 768, 44); } } -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self layoutAccordingToOrientation]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 

The following call is made:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

To get around this UIDeviceOrientationUnknown problem, I now use the following:

 UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { etc. } else { etc. } 

... which works every time.

However, I do not understand why the first option will not work in the context of a push-view controller. Anyone ideas? Is this just a mistake?

+7
source share
1 answer

I might think of a few things here, but I'm not sure if they are what you are looking for ...

First, when the application starts, typically the device orientation returns UIDeviceOrientationUnknown for performance reasons. One thing you can try is to place your routines inside .nib or in your storyboard with the right autoresizing options and let iOS do its best.

Secondly, if you are like me, maybe you are testing a device that is next to your computer, lying on a table. Well, in this case you are not to get the UIDeviceOrientationUnknown thing. In fact, you should check the UIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation]) . For example, if a device is laid on its back, it returns yes to this operator. It also means that when you use UIInterfaceOrientationIsLandscape(orientation) , you get the correct result for the wrong reason. UIInterfaceOrientationIsLandscape returns false not because the device orientation is UIDeviceOrientationPortrait , but because it is not valid.

Not sure if this helped, but it took me a while to figure it out, and I thought it would be nice to share this information! :)

+3
source

All Articles