美文网首页
iOS 页面横竖屏

iOS 页面横竖屏

作者: iVikings | 来源:发表于2017-11-10 10:48 被阅读10次

当App启动的时候需要强制竖屏,进入App后允许横屏或者竖屏的解决办法:

1、修改App-Info.plist文件只支持UIInterfaceOrientationPortrait方向,如下:
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
2、在AppDelegate.m文件中添加如下代码:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
3、新建一个导航控制器的分类,添加如下代码:
#import "UINavigationController+Orientation.h"

@implementation UINavigationController (Orientation)

/**
 *  导航控制器旋转问题
 *  只想在某一个类可以进行旋转,其他类不能旋转.
 *  扩展导航控制器方法,写以下几个方法
 */
/// 是否自动旋转
- (BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

/// 支持的方向
- (NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

/// 返回优先的方向 (此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
4、新建一个UITabBarController的分类,添加如下代码:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

/// 返回优先的方向 (此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotate {
    return self.selectedViewController.shouldAutorotate;
}
5、新建一个基类控制器,里面添加如下代码:
@property (nonatomic, assign, getter=isAutoRotate) BOOL autoRotate;

/// 是否自动旋转
- (BOOL)shouldAutorotate {
    return self.isAutoRotate;
}

/// 支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.isAutoRotate ? UIInterfaceOrientationMaskAllButUpsideDown : UIInterfaceOrientationMaskPortrait;
}

/// 返回优先的方向(此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

当某一个控制器需要进行旋转的时候,直接写self.isAutoRotate = YES 就可以啦~

【Github地址】https://github.com/huipengo/WBOrientation

相关文章

  • 关于iOS横竖屏适配

    关于iOS横竖屏适配 关于iOS横竖屏适配

  • iOS 页面横竖屏

    当App启动的时候需要强制竖屏,进入App后允许横屏或者竖屏的解决办法: 1、修改App-Info.plist文件...

  • iOS: 手机 支持屏幕 方向

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

  • [iOS]终极横竖屏切换解决方案

    [iOS]终极横竖屏切换解决方案 [iOS]终极横竖屏切换解决方案

  • iOS 横竖屏

    iOS横竖屏

  • 横竖屏

    关于iOS横竖屏适配[https://www.jianshu.com/p/1993144ea35e]IOS横竖屏以...

  • iOS实录11:代码处理iOS的横竖屏旋转

    [这是第11篇] 导语: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介...

  • iOS竖屏切到横屏

    1.AppDelegate 2、竖屏推到横屏(只横屏) 3、一个页面可横竖屏旋转 根据横竖屏更新UI

  • iOS的横、竖屏和旋转

    前言: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介绍监听屏幕旋转方向...

  • 适配面试

    图片的适配 应用图标 iOS 上传所需基本图片尺寸iOS App图标和启动画面尺寸 页面适配(横竖屏适配) Aut...

网友评论

      本文标题:iOS 页面横竖屏

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