What is the correct way to determine the orientation of an interface at startup?

[Question updated]

So, here is the problem, I finally narrowed it down. If you create a new UIViewController in all methods

- (id)init; - (void)loadView; - (void)viewDidAppear:(BOOL)animated; - (void)viewDidLoad; (...) 

Standard interface Orientation is a portrait, and if landscape mode is detected, it will quickly turn to that orientation. Which can be detected with:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; 

The problem is that on the iPad it is very difficult to prepare the interface for the current interface orientation in loadView (or one of the others), since it will only return the portrait. This will cause several problems:

1) I want my content to reload in portait mode, but NOT in landscape mode. Normally I would put an if statement in loadView. If in portrait mode, reload the content. But in this case, he will always return the portrait and, thus, always downloads the content.

2) I want to use the presentPopoverFromRect: inView: allowedArrowDirections: animated: 'method - in portrait mode so that it automatically displays a pop-up menu when the application starts. This will cause the application to crash when launched in portrait mode. Reason: "Popovers cannot be imagined from a point of view that does not have a window."

The only safe guess is inside 'didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation', but this method will not run if it starts in portrait mode.

// ----

Update (15.37)

'UIApplicationWillChangeStatusBarOrientationNotification'

It will be published only if the user switches from portrait to landscape (or vice versa). If the interface was a problem, then this can be easily solved by observing this notification and

 if (UIDeviceOrientationIsPortrait(interfaceOrientation)) { // layout subviews for portrait mode } else { // layout subviews for landscape mode } 

But the problem is that I want to know what mode it is at startup to determine if it is more likely or not. I have to reload the content, I can not reload the content and when it changes to landscape, cancel it.

+6
iphone ipad orientation
source share
3 answers

Try the [[UIApplication sharedApplication] statusBarOrientation] .

+8
source share

The correct way is to ask the device for its orientation, since the view is technically not oriented .;)

So you are using UIDevice . From the documentation you can see that first you need to generate notifications about the orientation of the device before this information is correct. Then it will work as desired.

 UIDevice *myDevice = [UIDevice currentDevice]; [myDevice beginGeneratingDeviceOrientationNotifications]; UIDeviceOrientation currentOrientation = [myDevice orientation]; [myDevice endGeneratingDeviceOrientationNotifications]; 

Note. This gives you the current orientation of the device. If the currently visible / top controller does not display the resolution of rotation to this orientation, you will nevertheless get the current orientation of the device, and not the one that is currently used in the top view.


Edit after question has been updated:

You are correct that the orientation for the top view controller (working correctly for subview controllers) always returns 1 (i.e. UIInterfaceOrientationPortrait ) in loadView . However, the willRotateToInterfaceOrientation:duration: method will be called immediately, and the orientation passed there is correct, so you can use this method.

 - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toIfaceOrient duration:(NSTimeInterval)duration 
+6
source share

I'm not sure if this will really solve my problem, but looking at the documentation, I found the following:

 typedef enum { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft } UIInterfaceOrientation; 

and

 typedef enum { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, UIDeviceOrientationPortraitUpsideDown, UIDeviceOrientationLandscapeLeft, UIDeviceOrientationLandscapeRight, UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown } UIDeviceOrientation; 

So, since FaceUp and FaceDown are added, looking at the orientation of the device, it is pointless for interface purposes. Therefore, theoretically, @MarkAdams correctly mentions that in this case [[UIApplication sharedApplication] statusBarOrientation] should be used to orient the interface.

Or, of course, the UIViewController has the "interfaceOrientation" option.

+1
source share

All Articles