UIViewController and UISplitViewController in UITabBarController shouldAutorotateToInterfaceOrientation

喜你入骨 提交于 2019-12-06 08:57:56

问题


i have some Problems with my iPad Code.

I have a UITabBarController which holds some UIViewController and a UISplitViewController. The problem is that the UIViewController and even the UISplitViewController dont recognize orientation Changes correctly.

i have set shouldAutorotateToInterfaceOrientation on my TabBarController and all UIViewControllers but i realized that only willRotateToInterfaceOrientation in the Top moast ViewController will fire which is my TabBarController. If i remove shouldAutorotateToInterfaceOrientation from my TabBarController willRotateToInterfaceOrientation from my sub UIViewControllers will get called. The biggest problem is my UISplitViewController, because it will rotate to the new interfaceOrientation but it is stucked in his Portrait Layout.

How do i correctly implement a TabBarController with ViewControllers and Splitviews including orientation changes?


回答1:


Hey i came up with a Workaround myself now. To Recap the Problem Only the first addet View to the Window will recognize Orientation Changes.

I Subclassed My TabBarController and made it ro Rotate to the Interface Orientation

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self adjustViewsForOrientation:toInterfaceOrientation];    
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape");
        //Do Your Landscape Changes here


    }
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@"Portrait");
        //Do Your Portrait Changes here
    }
}

But now the "viewControllers" of my TabBarController wont still recognize my InterfaceOrientations. So i came up with The folowing:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    for (int i = 0; i < [self.viewControllers count]; i++ ) {
        [[self.viewControllers objectAtIndex:i] didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    }
}

This will call the didRotateFromInterfaceOrientation Method from all Subclasses of the TabBarController:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self adjustViewsForOrientation:self.interfaceOrientation];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Subview Landscape");
        //Do Your Landscape Changes here
    }
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@"Subview Portrait");
        //Do Your Portrait Changes here
    }
}

As You can see i call [self adjustViewsForOrientation:self.interfaceOrientation]; in my Sub Viewcontroller which will give the actuall Orientation to the adjust method. If you use fromInterfaceOrientation it will be the wrong Orientation, because the change was already done!

My other problem was the UISplitviewController in TabBarController, but i dident got it working in a acceptable way. The problem is the same as for the UIViewControllers. It wont regocnize Orientation Changes so you have to Subclass it, but i dident get it working to 100%. As i searched the Web i found a good Code Example for a cutsom build Splitview. So ull maybe give it a shot: http://blog.trustedones.com/development/ipad-uisplitviewcontroller-replacement-for-sethidesmasterviewinportrait http://www.trustedones.com/apps/ipad

It also keeps the SplitView in Portrait Mode so you maybe will like it. I do!

Hope i could help someone with this post.. Cheers nettz




回答2:


Note that the second sentence of the UITabBarController class reference states "This class is not intended for subclassing."

So, while this approach may work, I suspect it is not the "correct" one. (I too am having this problem.)



来源:https://stackoverflow.com/questions/3316750/uiviewcontroller-and-uisplitviewcontroller-in-uitabbarcontroller-shouldautorotat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!