美文网首页
iOS开发中App个别页面支持横屏

iOS开发中App个别页面支持横屏

作者: d4d5907268a9 | 来源:发表于2024-07-17 16:59 被阅读0次

之前整个App是只支持竖屏的,一个功能性App要啥横屏啊,突然有一天有人心血来潮要在App中浏览文件的时候支持横屏,那就加吧,记录一下:
iOS App支持横竖屏切换需要在三个地方调整

  1. 工程文件中需要勾选Landscape Left和Landscape Right


    image.png
  2. App Delegate中需要实现- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window方法:

AppDelegate.h文件中添加属性allowRotate,用以告诉系统什么时候支持横屏

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, assign) BOOL allowRotate;

@end

AppDelegate.m文件中增加以下代码

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotate) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}
  1. 在需要支持旋转的页面testController.m中添加以下代码:
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 启用转屏功能
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // 关闭转屏功能
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = NO;
    // 离开页面时将App切回竖屏
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

做完以上操作之后,testController此时就已经支持转屏了,如果不能转屏,需查看一下手机控制中心的屏幕方向锁定开关是否打开。


image.png

以上这些做完后,多数情况下很可能是不够的,尤其是当我们的页面布局是使用frame写死的情况,如果是用Masonry或snapkit布局的很可能还会出现崩溃的问题issue491
有些文章会通过以下两个方法开启监听屏幕旋转来实现页面适配

UIKIT_EXTERN NSNotificationName const UIDeviceOrientationDidChangeNotification;
- (void)beginGeneratingDeviceOrientationNotifications;
- (void)endGeneratingDeviceOrientationNotifications;

但是这里我们不这样做,我们用另一种方式实现,屏幕旋转时会触发- (void)viewWillLayoutSubviews方法,我们在这里重新布局即可。

// 注意,我们在初始化时候已经添加约束
- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
}
...
...
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    // 注意这里是remake
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
          make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
          make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
          make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
          make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
}

至此,在一个竖屏App中个别页面实现转屏支持的操作已全部完成。

相关文章

  • iOS转屏控制分析

    在iOS开发中,绝大部分页面都只支持竖屏显示,只有个别页面要支持横屏显示,这种场景很常见。这篇文章我会介绍一下我在...

  • iOS页面旋转详解

    前言 在iOS开发中,如果APP需要支持横屏,就要控制页面旋转,但是让页面支持旋转的方式有很多,在此总结一下,说一...

  • iOS: 手机 支持屏幕 方向

    [经验]iOS app整体是竖屏(横屏),某个页面却支持横竖屏](http://blog.csdn.net/hhe...

  • iOS-屏幕旋转截屏相关

    本篇收录各种屏幕旋转知识点等. 1.详解iOS开发中处理屏幕旋转的几种方法2.iOS 个别页面强制横屏,其他页面竖...

  • iOS旋转屏幕设置总结

    iOS开发中,经常会碰到某些页面需要支持横屏显示,某些又仅支持竖屏显示,那怎么样才能比较完美的实现各个界面的横竖屏...

  • iOS 之强制横竖屏问题

    1、iOS开发中一般情况只会用到竖屏,但有些app 的个别页面却要变成横屏,所以在很竖屏的切换就是一个经常用到的问...

  • iOS开发中个别页面横屏操作实战

    阅读原文 iOS开发中常见的显示模式为竖屏,就是说不能旋转。当然也有旋转的需求,比如像阅读类,证券类,视频类App...

  • 横竖屏切换 (swift)

    一. 需求 APP中需要支持横屏和竖屏,并在不同的页面 可支持的屏幕旋转方向不一致 整体竖屏,部分强制横屏 整体横...

  • Autorotate - 让你的应用支持旋转

    iOS App大多数情况下都是只支持竖屏的,少部分页面才支持旋转,甚至有些页面需要强制横屏。本文将介绍应用如何支持...

  • 横竖屏

    横竖屏需求在开发中遇到一个特殊需求,在整个APP的大部分页面都支持竖屏模式,但在某些个别页面,如视频播放的页面或游...

网友评论

      本文标题:iOS开发中App个别页面支持横屏

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