views:

945

answers:

1

I'm strugging with getting an iPhone application which requires just about every push or pop in the Nav Controller Stack to change orientation.

Basically the first view is portrait, the second landscape the third portrait again (Yes I know this is less than ideal, but that's the design and I've got to implement it).

I've been through various advice on here....
http://stackoverflow.com/questions/995723/how-do-i-detect-a-rotation-on-the-iphone-without-the-device-autorotating
http://stackoverflow.com/questions/1824682/force-portrait-orientation-on-pushing-new-view-to-uinavigationviewcontroller
http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
But without total success.

Setting to link against 3.1.2 my reading of the linked articles above seems to indicate that if my portrait view pushes a view with

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
Then then that view should appear rotated to landscape. What happens is it appears in its "broken" portrait form, then rotates correctly as the device is turned.

If I pop the controller back to my portrait view (which has an appropriate shouldAutoRotate...) then that remains in broken landscape view until the device is returned to portrait orientation.

I've also tried removing all the shouldautorotate messages, and instead forcing rotation by transforming the view. This kind of works, and I've figured out that by moving the status bar (which is actually hidden in my application) [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight; the keyboard will appear with the correct orientation when desired.

The problem with this approach is that the status bar transform is weird and ugly when you don't have a status bar - a shadow looms over the page with each change.

So. What am I missing.

1) Am I wrong in thinking that in 3.1.2 (or possibly earlier) shouldAutorotateToInterfaceOrientation should provide the desired orientation simply by pushing controllers ?

2) Is there another way of getting keyboards to appear in the correct orientation.

3) Are the undocumented API calls the way to go (please no!)

A: 

You shouldn't use [UIViewController shouldAutorotateToInterfaceOrientation:] to trigger an orientation change; it's only there to let the system know if automatic rotations are allowed. You should still update it to specify the orientation that's allowed though.

If you want to change the orientation when a particular view is showing, you should call [UIApplication setStatusBarOrientation:animated:] inside your [UIViewController viewWillAppear:] override method for each of the view controllers that force a particular orientation. That will cause a change when a view is being pushed onto the stack and when it's being popped off it. Make sure you call super in your override method.

This is also the right place to change how the status bar is displayed, if that's something you're doing.

Tony
I'm not sure if there is a difference between [UIApplication setStatusBarOreintation.... and [UIApplication sharedApplication].statusBarOrientation... I'll test, otherwise that is what I have done, and I am suffering from the shadow of the status bar spinning around. In the landscape view, I use a transform to show the view in landscape initially. If I then allow a landscape view with shouldAutorotateToInterfaceOrientation, won't it get turned again ? How do I override that ?
Andiih