首页 > 代码库 > 当rootViewController为tabbarController时,控制屏幕旋转的方法

当rootViewController为tabbarController时,控制屏幕旋转的方法

在ios6以后,ios系统改变了屏幕旋转的方法,如果要设置屏幕旋转的方法,需要在rootvc里面进行编写,例如

UIViewController *viewCtrl = [[UIViewController alloc] init];  
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];  
if ([window respondsToSelector:@selector(setRootViewController:)]) {  
    self.window.rootViewController = navCtrl;  
} else {  
    [self.window addSubview:navCtrl.view];  
} 

当root为nav时,你要建立一个nav的子类,进行修改,如果是vc时,直接可以在vc里进行修改,网上已经有很多的例子了,但如果是tabbar里面嵌套这很多nav和vc,nav里又有vc我们要怎么弄呢,下面是我调研的一些方法,就是用几个category对nav和tabbarController进行类别的编写,让他们可以分别对应子视图的旋转方向

UITabBarController+autoRotate

@interface UITabBarController (autoRotate)

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

@end


#import "UITabBarController+autoRotate.h"

@implementation UITabBarController (autoRotate)

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations];
}

@end

UINavigationController+autoRotate.h

@interface UINavigationController (autoRotate)

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

@end

@implementation UINavigationController (autoRotate)

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

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

@end


UIViewController1.m

- (BOOL)shouldAutorotate {
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;;
}
UIViewController2.m

- (BOOL)shouldAutorotate {
   return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskAll;
}


参考文章:IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统) , 在IOS应用中从竖屏模式强制转换为横屏模式 , http://stackoverflow.com/questions/12522903/uitabbarcontroller-rotation-issues-in-ios-6


当rootViewController为tabbarController时,控制屏幕旋转的方法