I have a UIViewController with IBOutlets linking to several UIViews. Various buttons switch between the views, but there are issues when I set a new view for the controller. Both the controller and the views are in landscape orientation, but after the first couple of switches some of the views display in portrait mode. What might be causing this?
-
Just a guess, that there is code in the default implementation of viewWillAppear that detects the orientation and prepares the view for landscape mode. What if before you change the view, you call the new view's viewWillAppear? – morningstar Aug 28 '11 at 18:14
-
UIView doesn't have a -viewWillAppear method. I did what you suggested with the UIViewController's -viewWillAppear, but no change. – Jumhyn Aug 28 '11 at 18:26
-
Are all of your views supposed to be in Landscape, or are some of them in Portrait? – msgambel Sep 01 '11 at 20:17
-
All of them are landscape in the .xib, and I don't allow rotation to portrait orientations. – Jumhyn Sep 01 '11 at 23:06
-
You might also check out my answer to a similar question (also a bounty): [Ipad orientation is not working well](http://stackoverflow.com/questions/7007930/ipad-orientation-is-not-working-well/7097854#7097854) – Sam Sep 07 '11 at 18:25
4 Answers
You should check that all your UIViewControllers implement method shouldAutorotateToInterfaceOrientation
. This method tells the operation system in what positions (orientations of screen) the UIViews
controlled by that UIViewControllers could be displayed.
If you want all your views be displayed only in landscape orientations, then that method should return YES
only for interfaceOrintation == UIInterfaceOrientationLandscapeLeft
or interfaceOrintation == UIInterfaceOrientationLandscapeRight
. if you want only portrait orientations then interfaceOrintation == UIInterfaceOrientationPortrait
.
For example (support only landscape modes) :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
And you should check that in your project settings you set appropriate Supported Device Orientations (also known as Supported interface orientations).

- 17,837
- 1
- 55
- 65
-
Yes, I have done all of this. As I said, it displays fine once, and then stops working after a couple of switches. – Jumhyn Sep 01 '11 at 23:05
I think you should set the views to landscape mode in IB. If this is already done then I would suggest not changing the view controller's view explicitly. Implement a method, which can change the views.
In more detail:
Set a view as your view controller's view then every other view should be the subview of this view. You can make the change by removing the presented view and adding the new view or another method to implement the view changing is to add all view's in IB as a subview, hide all views then when you would like to change unhide the desired one and hide every other.
I hope you understand my approach, if not then I am here to answer your questions.
This way is easy to implement. :)

- 7,415
- 13
- 55
- 118
-
I tried the way of removing and adding subviews, although I like your approach of hiding and unhiding the views. I'll try that tomorrow and get back here before the bounty expires if it works :P – Jumhyn Sep 06 '11 at 06:25
-
no, sorry. I guess ill look for some real world help since this situation is pretty specific. – Jumhyn Sep 16 '11 at 02:18
Check that you have this method enabled in all views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Also, is it what you see on the simulator or on your device?

- 4,145
- 10
- 42
- 66
-
-
You just need to copy and paste this code to each view controller for it to take effect. – TommyG Aug 28 '11 at 17:24
-
Yeah I already have it in the UIViewController. I just didn't understand what you meant by adding it to the view. Any other ideas? – Jumhyn Aug 28 '11 at 17:30
-
It's a UIViewController method, you can't add it to separate UIViews. – Filip Radelic Sep 02 '11 at 08:35
This may be a Simulator bug, but try using separate objects of UIViewController for each view, instead of one controller for all views. Personally, I haven't experienced any difficulty with orientation before, but I do things programmatically. I recommend that you do the same (you may NOT need to convert all your program's xib files, but maybe just the one you're having problems with). Just make sure that all UIViewControllers that you're using for that view to have the:shouldAutorotateToInterfaceOrientation
set up appropriately and things should work just fine.
Hope this helps.

- 549
- 2
- 7
- 16