Alert views in portrait, iPad in landscape, and unable to rotate to anything but “other” landscape (180 degrees)

StackOverflow https://stackoverflow.com/questions/3816566

Question

Update: Solved. See fix at bottom.

In my iPad app which supports all orientations, if the app is loaded in landscape, weird things happen:

  1. Alert views appear in portrait, even though the rest is in landscape
  2. Rotating the iPad from landscape to portrait does not trigger a rotation (nor does it trigger any of the shouldAutorotate style methods).
  3. Rotating the iPad 180 degrees (to "other landscape") does trigger rotation, and after this, the iPad behaves like normal (i.e. rotates correctly to portrait and landscape when rotated).

Alert view screen below. Weird, weird. Any ideas why this might be happening? It only happens when loading in landscape -- loading in portrait and then rotating to landscape works just fine. Landtrait!

Solution: This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear: method).

iOS sends a number of (4) shouldAutorotateToInterfaceOrientation: calls to the current/main view controller after things have started up, to figure out which orientations are currently supported. When I added the other VC so soon, this occurred in the middle of the aforementioned calls. Thus, I only got one or two calls (for "portrait", because it starts with portrait).

The main view controller was then left with a default support of "NO" for the landscape orientations, but the newly added view controller (which is also asked about orientations) now happily responds yes.

If I had removed the other VC (which is in landscape), I would have realized that my main view controller was stuck in portrait. So I had a portrait view, on top of which there was a landscape view. The alerts and such heeded the orientation of the former, hence the disastrous results above.

Additionally, if I tried to rotate to portrait, I couldn't, because the main view controller was already in portrait. Rotating to landscape was also impossible because the other view controller is already in landscape. Only when I did the "landscape but that other one" rotation, did both view controllers agree that rotation was required, and thus the device rotated.

The fix in my case was to simply put the creation of the other controller at a slight delay. using performSelector:withObject:afterDelay. -- 0 seemed to work but I just put it at 1 to be sure. It looks a bit weird, with the main vc appearing and then the other vc, but it's better than the above.

Was it helpful?

Solution

This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear: method).

See above post for details on solution.

OTHER TIPS

Use UIAlertController instead of UIAlertview. I have the same problem and its now fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top