开发者

Autorotation and multiple view controllers

I'm creating an iPad application which should only work in portrait and portait upsidedown modes. For performance reasons in my applicationDidFinishLaunching method I'm creating several viewControllers, and adding them to my main window as subviews. I then hide the ones I don't want to see straight away. There is no tab bar or navigation control开发者_运维知识库ler.

My problem is that only the first viewController seems to be receiving the rotate calls. I have verified this by swapping around the order in which I add the subviews to the main window and NSLog's. Is there some way I can force all the controllers to receive the calls?

Some of my views are designed to lay over the top of another view, but this behind view will not always be the same one - so it seems to make sense to have the overlay view in a separate view controller. Am I doing something fundamentally wrong, and that's why it's not behaving as I would expect?

EDIT: The accepted answer for this question seems to indicate the exact problem I'm facing: UIViewController rotate methods

EDIT 2: Another question that confirms only the "primary" view controller will receive the rotate events. Is it really the case that I need to put all my code into one view controller that has multiple views? Multiple view controllers on screen at once?


For someone who comes across this in the future, I solved this by moving items out of the app delegate and into a single view controller. This view controller then contains all the relevant sub-view controllers. By only having the primary view controller as a direct subview of the app delegate window, autorotation went back to working as expected.


In AppDelegate you must using

[self.window setRootViewController:_controller];

instead

[self.window addSubview:_controller.view];

And controller must override method

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


There is another solution, depending on your design scenario. I had a similar problem, whereby I needed to swap to overlaying view controllers, both of which had to support autorotation.

The solution was very simple. Just put the controllers under different window objects.

All UIWindow object receive device orientation events and propagate them to their sub-view controllers.

So, in the end I just ended up swapping UIWindow(s) and it works perfectly.


I ran into this page looking for a way to fix this problem for an iPad application, and I just want to share what I did to fix this.

I put the following code inside my MainViewController (which is my rootController):

-(void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration {
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            // My app has a toolbar, use CGRectMake(0, 0, 1024.0, 768.0) if your's doesn't 
            self.view.frame = CGRectMake(0, 20, 1024.0, 748.0);
            break;

        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            self.view.frame = CGRectMake(0, 20, 768.0, 1004.0);            
            break;

        default:
            break;
     }
}

After doing this, my components were repositioned as per the Autosizing settings that I put in Interface Builder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜