iPad’s Initial Orientation Issues

Last week, I had to make an iPad application with 3 viewcontrollers’ views in the window.

It’s an unofficial convention that you should have only ONE view controller at the root level, and then all the other controllers should be descendants of this one. Then the orientation, transformations and the rest is passed along the view hierarchy through this root controller.

For a reason best left aside, I had to have 3 “root” view controllers, that would animate in and out, replace each other on occasion etc. I felt it would be easier to code and maintain having them be installed in the window at the same time.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
  [self.window addSubview:vc1.view];
  [self.window addSubview:vc2.view];
  [self.window addSubview:vc3.view];
  // ...

Now, the twist is that my application can only run in landscape mode (with the relevant information in the plist set).

First launch, the views are correctly created and put on screen, but 2 of them are displayed in portrait mode, while the first one is indeed in landscape. The xibs are all set in landscape, the sizes are correct, but the display is not.

After some digging around, I setup the shouldAutorotate delegate methods, and check upon display the orientation the view controllers think they are in.

Surprise, it does indeed work, the orientation is set correctly, and the views are resized to landscape mode. But still displayed with the wrong orientation. The UI is setup in portrait mode, then stretched in width and shrunk in height.

At this point in time, I guess I could have considered to put all my view controllers as descendant of the root (first) one, and let the rotation/stretching be handled by it. Or created a blank root one in which the other three would fit.

After some more digging, helped hugely by Dam, it turns out all the information and notifications but the transformation matrix are passed along all the root view controllers. Therefore it’s just a matter of forcing it. For those of you having the same problem, it’s as easy as:

vc2.view.transform = vc1.view.transform;
vc3.view.transform = vc1.view.transform;

Bug? Feature? Up to you to decide.


Leave a Reply