开发过程中,总是遇到一个或多个ViewController需要支持屏幕旋转,而其他不需要的情况,故需要特殊处理需要旋转的ViewController,而不影响其他。
iOS的屏幕旋转是由根视图控制的,只要重写根视图的-(BOOL)shouldAutorotate
和-(UIInterfaceOrientationMask)supportedInterfaceOrientations
方法即可,当前举例以UITabBarController+UINavigationController:
选择Device Orientation
新建类别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默认仅支持垂直方向,若有支持多方向需求,再单独重写Controller的-(BOOL)shouldAutorotate
和-(UIInterfaceOrientationMask)supportedInterfaceOrientations
方法即可。