5

I have been reading a ton on rotation, but not finding a solution to my query. Here goes:

I have a portrait application with a tabbar and hidden navigation controller in my tab. At a point in the app, the next view must be landscape.

The XIB layout has been done in landscape, so I want to bring up the xib without any translation or pixel moving code. (not thinking this is required) I have tried just pushing the view (remains in portrait), shifting the view using various methods (non seem to line thing up properly).

Is there a way to tell the view that it is already laid out for landscape prior to it being opened?

Thanks in advance!

Rob Bonner
  • 9,276
  • 8
  • 35
  • 55
  • Find an answer to this question [here][1] [1]: http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation – Mark Jul 28 '11 at 13:36
  • More specifically, look at the 77-upvotes answer on that question: http://stackoverflow.com/a/4915378/39974 - pushing and popping a dummy view controller seems to work well at least up to and including iOS 5.1. – Ivan Vučica Aug 08 '12 at 12:48

2 Answers2

7

Found it, this code does the trick in the viewdidload:

self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);

Still have 1 odd thing. No matter what I do to set the navigation bar to hidden, it does not rotate, and stays at the left side of the view.

[[self navigationController] setNavigationBarHidden:YES animated:NO];

Has anyone seed this behavior and solved?

Rob Bonner
  • 9,276
  • 8
  • 35
  • 55
3

I'm positive that you cannot 'force' a rotation. The system decides when to change the orientation of the device; so the 'orientation' properties are essentially read-only. I looked into this same problem a long time ago when I wanted to make sure a particular view always displayed in one orientation.

Due to this, most apps allow all of their views and view controllers to work in any of the orientations the app supports. I've found that trying to restrict the behavior of some views and view controllers ultimately creates more hassle, and can cause issues when transitioning between views and view controllers.

The code you posted will work for your view. You are not actually changing the orientation at all; your view is just behaving like it has been rotated by drawing in a rotated fashion. I'm not sure if you can do the same thing to the navigation bar or not, but it's worth a shot. If you are able to control the view properties of the navigation bar (it is a UIView as well), applying the same pattern you are using for your custom view should work.

Most apps that want a view to only be in landscape ultimately force their entire app to be in landscape. For instance, Flight Control only supports one orientation. Thus, the drawing code is pretty simple; regardless of orientation, just draw the view and rotate it to the one orientation it supports (either landscape left or right).

Your app's design wouldn't be that easy... it sounds like you are not designing a full-screen app. You would have to worry about the navigation bar and status bar being properly drawn.

CJ.
  • 1,034
  • 1
  • 9
  • 20