iOS 指定部分页面可屏幕旋转

以下转载在简书 ,原文链接:http://www.jianshu.com/p/11fe95ec1159

需求

让app中某一个或者某几个页面可以支持屏幕旋转,但是登录页面,主页面等大部分页面不支持屏幕旋转。

开干

第一步 设置app可以支持屏幕旋转

在Xcode project的general这个标签下,设置app支持的屏幕旋转方向。

iOS 指定部分页面可屏幕旋转_第1张图片

或者可以在在info.plist文件中设置Supported interface orientations


第二步 设置app不支持屏幕旋转

这里是在代码中设置app中所有的页面不支持屏幕旋转,(然后在个别需要旋转的页面再另设置)

需要在一个app的每个view controller中,可以分别指定当前view controller支持的旋转方向。但是这里所指的view controller,必须是root view controller或者全屏的view controller,不然相关方法是不会被调用的。

所以说,如果这个view controller是被包含在navigation controller或者tab bar controller中的时候,无法设置。需要在自定义的navigation controller或者tab bar controller中设置支持的旋转方向。

在自定义的navigation controller或者tab bar controller中重写supportedInterfaceOrientations方法


- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

这样,设置后,当前app所有页面不支持屏幕旋转。

第三步 设置部分页面可以旋转

来到需要设置的ViewController重写supportedInterfaceOrientations方法

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft;  //支持横向
}

//设置为允许旋转
- (BOOL) shouldAutorotate {
   return YES;
}



你可能感兴趣的:(iOS)