码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-04-30 22:24:39      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:ios   uitabbarcontroller   旋转   屏幕旋转   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时,控制屏幕旋转的方法

标签:ios   uitabbarcontroller   旋转   屏幕旋转   tabbarcontroller   

原文地址:http://blog.csdn.net/ioswyl88219/article/details/24711197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!