美文网首页
代码控制屏幕横竖旋转

代码控制屏幕横竖旋转

作者: Mariko00o | 来源:发表于2020-09-18 17:01 被阅读0次

    屏幕旋转控制分动态旋转和手动代码控制旋转,多见于视频播放。

    一 .APP设置

    1. 在配置中关闭屏幕方向。


      info.plist
    2. AppDelegate添加如下方法 返回所需要的旋转方向。代码设置的方向优先级将大于info.plist中的设置。

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    1. UITabBarController方向由选中的controller的方向控制,在UITabBarController中添加如下设置:
    - (BOOL)shouldAutorotate
    {
        return [self.selectedViewController shouldAutorotate];
    }
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return   [self.selectedViewController preferredInterfaceOrientationForPresentation];;
    }
    
    1. UINavigationController中由topViewController方向决定
    - (BOOL)shouldAutorotate {
        return [self.topViewController shouldAutorotate];
    }
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return [self.topViewController supportedInterfaceOrientations];
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    

    二. 旋转控制

    1. 在需要动态控制旋转的controller配置:
    //是否支持自动转屏
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    // 支持哪些屏幕方向
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    // 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return   UIInterfaceOrientationPortrait;
    }
    
    1. 手动控制旋转
    /// 旋转
    /// @param aUIDeviceOrientation 需要设置的旋转方向
    -(void)changeWithDeviceOrientation:(UIDeviceOrientation)aUIDeviceOrientation{
        UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
        if (aUIDeviceOrientation != deviceOrientation) {
            if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
                [[UIDevice currentDevice] setValue:@(aUIDeviceOrientation) forKey:@"orientation"];
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:代码控制屏幕横竖旋转

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