美文网首页
屏幕旋转

屏幕旋转

作者: 傲骨天成科技 | 来源:发表于2019-12-12 17:40 被阅读0次

    本文涉及到的转屏是咱们的app的某个页面设置横竖屏的切换

    • 必须先在appdelegate中实现下面的方法
      -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
       }
      
      
    • 在对应的ViewController中重写以下三个方法:

      1. 支持屏幕旋转
          - (BOOL) shouldAutorotate {
        
                   retunrn YES;
          }
        

      2.当前viewController支持的设备方向

       -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
        if (self.a == 0) {
      
          return UIInterfaceOrientationMaskPortrait;
      }else {
      
          return UIInterfaceOrientationMaskLandscape;
      } 
      }
      

      其中的a是当前VC的一个全局属性,用于你控制旋转屏幕的时候支持的屏幕方向。

      1. 默认进入到当前的VC的时候显示的方向
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
       return UIInterfaceOrientationPortrait; 
    }  
    
    1. 状态栏是否隐藏
     - (BOOL)prefersStatusBarHidden {
     return NO; // your own visibility code
    }
    
    

    5.点击某个按钮进行横屏

    
    - (void)switchToLandscape
    {
     self.a = 1;
     [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
     [UIViewController attemptRotationToDeviceOrientation];
    
    }
    
    

    6.点击某个按钮进行竖屏

     - (void)switchToPortrait
    {
      self.a = 0;
      [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
      [UIViewController attemptRotationToDeviceOrientation];
    }
    
    

    相关文章

      网友评论

          本文标题:屏幕旋转

          本文链接:https://www.haomeiwen.com/subject/llvtnctx.html