何時rootViewController至tabbarController時刻,控制屏幕旋轉法

於ios6後,ios系統改變了屏幕旋轉的方法,假設您想將屏幕旋轉法,在需求rootvc裏面製備,例如html

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
ios

@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-6ui


版權聲明:本文博主原創文章。博客,未經贊成不得轉載。spa

相關文章
相關標籤/搜索