開(kāi)發(fā)過(guò)程中勋锤,總是遇到一個(gè)或多個(gè)ViewController需要支持屏幕旋轉(zhuǎn)骇塘,而其他不需要的情況捉片,故需要特殊處理需要旋轉(zhuǎn)的ViewController,而不影響其他艺骂。
iOS的屏幕旋轉(zhuǎn)是由根視圖控制的诸老,只要重寫(xiě)根視圖的-(BOOL)shouldAutorotate
和-(UIInterfaceOrientationMask)supportedInterfaceOrientations
方法即可,當(dāng)前舉例以UITabBarController+UINavigationController:
選擇Device Orientation
新建類(lèi)別UITabBarController+Orientation
@implementation UITabBarController (Orientation)
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
@end
UINavigationController+Orientation
@implementation UINavigationController (Orientation)
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
@end
UIViewController+Orientation
@implementation UIViewController (Orientation)
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
這樣處理之后钳恕,UIViewController默認(rèn)僅支持垂直方向别伏,若有支持多方向需求蹄衷,再單獨(dú)重寫(xiě)Controller的-(BOOL)shouldAutorotate
和-(UIInterfaceOrientationMask)supportedInterfaceOrientations
方法即可。