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

代码控制屏幕横竖旋转

作者: 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"];
        }
    }
}

相关文章

  • 代码控制屏幕横竖旋转

    屏幕旋转控制分动态旋转和手动代码控制旋转,多见于视频播放。 一 .APP设置 在配置中关闭屏幕方向。info.pl...

  • iOS 屏幕旋转控制

    /** 屏幕旋转控制 allowRotateType ==(0强制竖屏,1横竖屏,2~强制横屏 )*/ (UIIn...

  • MG--Swift3.x干货( 转屏 跳转 RunTime

    代码下载 直播喵播MGMiaoBo下载 自动旋转--横竖屏控制(Swift3.0) appDelegate代码 控...

  • iOS屏幕旋转(横竖屏)

    一、屏幕旋转方向监听 1、UIDeviceOrientation:设备方向 iOS 定义了七种设备方向: 当设备方...

  • Swift 禁止屏幕旋转

    禁止屏幕旋转 在主控制器文件中添加如下代码: 敲重点禁止屏幕旋转的原理是覆写了控制器类(如果是新建的项目为View...

  • iOS 横竖屏切换

    网上关于横竖屏切换的资料很多,但是很容易踩到坑,不是屏幕不旋转,就是屏幕旋转后没有状态栏等,在写的小demo里屏幕...

  • iOS之屏幕旋转

    检测屏幕旋转:视图控制器本身能检测到屏幕的旋转,如果要处理屏幕旋转,需要重写几个方法: 视图控制器中的方法: 视图...

  • 屏幕旋转和弹出框

    iOS中控制屏幕旋转相关方法 shouldAutorotate:是否支持屏幕旋转 alertView:clicke...

  • 如何使用代码处理 iOS 的横竖屏旋转问题!!!

    一、监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...

  • Android屏幕横竖屏切换和生命周期

    一、屏幕横竖屏切换的代码 (一)设置屏幕横屏代码 (二)设置屏幕竖屏的代码 (三)判断屏幕是横屏还是竖屏的状态 这...

网友评论

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

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