iOS 6 ViewController is rotating but shouldn't

2019-02-05 01:17发布

I want several of my app viewcontrollers to not rotate in iOS 6.0. This is what i did to make the rotation in iOS 6 possible:

1.) Set the windows rootviewController in application:didFinishLaunchingWithOptions:

self.window.rootViewController = self.tabBarController;

2.) Set the "Supported Interface Orientations" in my Target (in XCode) so i can use all orientations

3.) Implemented the new iOS 6.0 rotation functionality

- (BOOL) shouldAutorotate {

    return YES;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;
}

4.) For some reasons, i subclassed the UINavigationController and implemented also these new functionalities and used this new NavigationController in stead of the original one.

So far so good, everything works fine and all viewcontrollers are now able to rotate to every orientation. Now i want several viewController to not rotate and only stay in portrait. But when i set the new rotations methods in those specific viewcontrollers like this, it still rotates to every orientation:

- (BOOL) shouldAutorotate {

    return NO;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}

Also setting the navigationController's rotationsfunctionality like above doesn't change anything. (All viewcontrollers can rotate to every orientation)

What am i doing wrong?

EDIT:

Also setting the preferred Interfaceorientation doesn't change anything:

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationMaskPortrait;
}

3条回答
Evening l夕情丶
2楼-- · 2019-02-05 01:55

This works for me:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}
查看更多
姐就是有狂的资本
3楼-- · 2019-02-05 02:01

You need to create category of UITabBarController to support should auto rotate

code of .h file is as

@interface UITabBarController (autoRotate)<UITabBarControllerDelegate>

    -(BOOL)shouldAutorotate;
    - (NSUInteger)supportedInterfaceOrientations;

@end

code of .m file is as

-(BOOL)shouldAutorotate {

    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication]delegate];
    return [delegate.tabBarController.selectedViewController shouldAutorotate];
}


- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

Note: Name of AppDelegate will be changed with your project's AppDelegate File name.

查看更多
三岁会撩人
4楼-- · 2019-02-05 02:02

If you want all of our navigation controllers to respect the top view controller you can use a category. I've found it easier than subclassing.

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
查看更多
登录 后发表回答