美文网首页
iOS在某个页面强制横屏

iOS在某个页面强制横屏

作者: 爪爪123 | 来源:发表于2020-06-19 14:00 被阅读0次

原文地址:http://www.cocoachina.com/articles/897984?filter=shuju
最近在网上找了很多关于如何在某一个页面强制横屏的资料,但是还是没有实现预期的效果。最后经过不懈努力,最终还是找到了一个可以实现这一需求的方法。下面就将该方法整理出来,并附带demo下载。供以后在开发中方便使用。

方法实现部分

在AppDelegate.h中定义一个属性,如下:

/// 是否允许转向
@property(nonatomic,assign)BOOL allowRotation;

AppDelegate.m中实现横屏或竖屏的设置:

///// 如果属性值为YES,仅允许屏幕向左旋转,否则仅允许竖屏。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (self.allowRotation == YES) {

        // 横屏
        return UIInterfaceOrientationMaskLandscape;
    } else {

        // 竖屏
        return (UIInterfaceOrientationMaskPortrait);
    }
}

在UIDevice分类中实现强制转屏,如下:

/// 输入要强制转屏的方向
/// @param interfaceOrientation 转屏的方向
+ (void)deviceMandatoryLandscapeWithNewOrientation:(UIInterfaceOrientation)interfaceOrientation {

    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

    // 将输入的转屏方向(枚举)转换成Int类型
    int orientation = (int)interfaceOrientation;

    // 对象包装
    NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];

    // 实现横竖屏旋转
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

应用部分

关于应用部分的代码,在需要旋转屏幕的地方调用如下方法:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    // 打开横屏开关
    appDelegate.allowRotation = YES;
    // 调用转屏代码
    [UIDevice deviceMandatoryLandscapeWithNewOrientation:UIInterfaceOrientationLandscapeRight];

在返回到上一个页面的时候,如果需要保持原来的竖屏,那么就应该实现对应的方法即可:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    // 关闭横屏仅允许竖屏
    appDelegate.allowRotation = NO;
    // 切换到竖屏
    [UIDevice deviceMandatoryLandscapeWithNewOrientation:UIInterfaceOrientationPortrait];

    [self.navigationController popViewControllerAnimated:YES];
好了,代码的主要实现部分都已经完成了,高高兴兴的进入调试阶段,结果怎么也不能实现预期效果。这又是什么原因呢,难道是代码层面还有什么问题没处理好?仔细检查了一下,原来是下面的原因: 截屏2020-06-19下午1.56.23.png

没错,你只要需要将Requires full screen这个选项钩上就大功告成了。

相关文章

  • iOS在某个页面强制横屏

    原文地址:http://www.cocoachina.com/articles/897984?filter=shu...

  • iOS在某个页面强制横屏

    最近在网上找了很多关于如何在某一个页面强制横屏的资料,但是还是没有实现预期的效果。最后经过不懈努力,最终还是找到了...

  • iOS在某个页面强制横屏

    最近在网上找了很多关于如何在某一个页面强制横屏的资料,但是还是没有实现预期的效果。最后经过不懈努力,最终还是找到了...

  • 编程技巧汇总

    iOS - 强制某个页面横屏,返回竖屏:https://www.jianshu.com/p/45ca13046ee...

  • 横屏竖屏切换

    项目只有竖屏的时侯,在某个页面强制横屏模态的形式推入: 在此页面进行强制转屏 转屏页面 push:推入此类继承UI...

  • 某个页面强制横屏

    http://blog.csdn.net/jayant_y/article/details/71077886WKW...

  • iOS 强制横屏(Push和模态)

    # iOS 强制横屏(Push和模态) iOS开发过程中,有时候需要页面强制横屏。 下面这种方法是不管手机有没有开...

  • iOS - 强制某个页面横屏,返回竖屏

    第一步: 首先在工程文件里设置如下: 第二步: 在AppDelegate中添加方法关闭横竖屏切换,方法如下 App...

  • ios强制横屏

    在强制横屏的页面重新加载init方法:(ios8后会隐藏状态栏) ios8 以后横屏状态栏不显示 解决方法:

  • iOS强制横屏

    iOS强制横屏

网友评论

      本文标题:iOS在某个页面强制横屏

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