iOS屏幕旋转

开发过程中,总是遇到一个或多个ViewController需要支持屏幕旋转,而其他不需要的情况,故需要特殊处理需要旋转的ViewController,而不影响其他。

iOS的屏幕旋转是由根视图控制的,只要重写根视图的-(BOOL)shouldAutorotate-(UIInterfaceOrientationMask)supportedInterfaceOrientations方法即可,当前举例以UITabBarController+UINavigationController:

选择Device Orientation


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方法即可。

Demo下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容