IOS在指定的控制器支持屏幕旋转

来源:互联网 发布:php 类方法是否存在 编辑:程序博客网 时间:2024/05/18 02:17
  • 想要app支持屏幕旋转,info.plis文件必须勾选支持旋转的几个选项:
    image

  • 控制屏幕旋转的三个方法:

- (BOOL)shouldAutorotate;- (UIInterfaceOrientationMask)supportedInterfaceOrientations;- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
  • 第一个方法决定是否支持多方向旋转屏,如果返回NO则后面的两个方法都不会再被调用,而且只会支持默认的UIInterfaceOrientationMaskPortrait方向;

  • 第二个方法直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown,官方文档有说明

  • 第三个方法返回最优先显示的屏幕方向,比如同时支持Portrait和Landscape方向,但想优先显示Landscape方向,那软件启动的时候就会先显示Landscape,在手机切换旋转方向的时候仍然可以在Portrait和Landscape之间切换;

  • 在UINavigationController或者自定义的UINavigationController中重写上面三个方法,即可实现在特定的控制器实现屏幕旋转代码如下:

- (BOOL)shouldAutorotate{ // 返回当前显示的viewController是否支持旋转    return [self.visibleViewController shouldAutorotate];}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{// 返回当前显示的viewController支持旋转的方向    return [self.visibleViewController preferredInterfaceOrientationForPresentation];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations {// 返回当前显示的viewController是优先旋转的方向    if (![self.visibleViewController isKindOfClass:[UIAlertController class]]) {//iOS9 UIWebRotatingAlertController        return [self.visibleViewController supportedInterfaceOrientations];    }else{        return UIInterfaceOrientationMaskPortrait;    }}
  • 然后在想要旋转屏幕的的控制器中重写上面三个方法:
- (BOOL)shouldAutorotate {   return YES;}// 这里返回需要支持旋转的方向- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{    return UIInterfaceOrientationLandscapeLeft;}// 优先显示的方向- (UIInterfaceOrientationMask)supportedInterfaceOrientations {    return UIInterfaceOrientationMaskAll;}
0 0
原创粉丝点击