返回 导航

Swift

hangge.com

Swift - 实现整体竖屏显示,而部分页面强制横屏(或自动横竖屏)

作者:hangge | 2018-10-19 08:13
    默认情况下,App 会根据屏幕旋转方向,自动切换成横屏或竖屏显示。如果想让 App 在任何情况下都强制横屏、或者竖屏显示,只需修改项目的“Device Orientation”配置项即可,这个可以参考我之前写的文章:

但这样设置后所有的页面都统一变成固定横屏或竖屏,而有的时候我们需要实现不同页面使用不同的效果,比如:
  • 整体竖屏显示,部分页面允许横竖屏切换(或强制横屏)
  • 整体横屏显示,部分页面允许横竖屏切换(或强制竖屏)
下面通过样例演示这个功能如何实现。

一、准备工作

(1)首先样例 Demo 中有如下 3 个视图控制器,点击主视图控制器的按钮可以分别跳转到后两者上:
  • ViewController.swift:主视图控制器
  • DetailViewController1.swift:详情页1视图控制器
  • DetailViewController2.swift:详情页2视图控制器

(2)默认情况下不做任何设置的话,这三个页面都是可以随着屏幕旋转方向改变,自动变成横屏或竖屏显示。

二、整体竖屏显示,部分页面允许横竖屏切换(或强制横屏)

1,效果图

(1)“首页”为默认的强制竖屏,任何情况下界面都是竖屏显示。
            

(2)“详情页1”为自动切换,会根据设备旋转方向自动变成横屏或者竖屏显示。
            

(3)“详情2”为强制横屏,任何情况下界面都是横屏显示。
            

2,样例代码

(1)AppDelegate.swift
  • 在这里增加一个变量 interfaceOrientations 表示当前支持的横竖屏方向(后面那些需要特殊处理的部分页面中会改变这个值)。
  • supportedInterfaceOrientationsFor 这个代理方法则返回当前支持的方向。
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    //当前界面支持的方向(默认情况下只能竖屏,不能横屏显示)
    var interfaceOrientations:UIInterfaceOrientationMask = .portrait{
        didSet{
            //强制设置成竖屏
            if interfaceOrientations == .portrait{
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
                                          forKey: "orientation")
            }
            //强制设置成横屏
            else if !interfaceOrientations.contains(.portrait){                
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,
                                          forKey: "orientation")
            }
        }
    }

    //返回当前界面支持的旋转方向
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor
        window: UIWindow?)-> UIInterfaceOrientationMask {
        return interfaceOrientations
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions
        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    }

    func applicationWillTerminate(_ application: UIApplication) {
    }
}

(2)ViewController.swift(主页面)
  • 由于 AppDelegate 中设置了默认强制竖屏显示,那么主视图控制器这边没有什么需要修改的。

(3)DetailViewController1.swift(详情页1)
import UIKit

//详情页1
class DetailViewController1: UIViewController {
    
    //获取 AppDelegate 对象
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //该页面显示时可以横竖屏切换
        appDelegate.interfaceOrientations = .allButUpsideDown
    }
        
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //页面退出时还原强制竖屏状态
        appDelegate.interfaceOrientations = .portrait
    }
}

(4)DetailViewController2.swift(详情页2)
import UIKit

//详情页2
class DetailViewController2: UIViewController {

    //获取 AppDelegate 对象
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //该页面显示时强制横屏显示
        appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //页面退出时还原强制竖屏状态
        appDelegate.interfaceOrientations = .portrait
    }
}

附:整体横屏显示,部分页面允许横竖屏切换(或强制竖屏)

(1)如果想让页面默认情况下都是横屏显示,则只需将上面 AppDelegate.swift 代码中的 interfaceOrientations 变量的默认值做个修改即可:
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    //当前界面支持的方向(默认情况下只能横屏,不能竖屏显示)
    var interfaceOrientations:UIInterfaceOrientationMask = [.landscapeLeft, .landscapeRight]
    {
        didSet{
            print(UIDevice.current.orientation.isLandscape)
            //强制设置成竖屏
            if interfaceOrientations == .portrait{
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
                                          forKey: "orientation")
            }
            //强制设置成横屏
            else if !interfaceOrientations.contains(.portrait){                
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,
                                          forKey: "orientation")
            }
        }
    }

    //返回当前界面支持的旋转方向
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor
        window: UIWindow?)-> UIInterfaceOrientationMask {
        return interfaceOrientations
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions
        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    }

    func applicationWillTerminate(_ application: UIApplication) {
    }
}

(2)如果部分页面允许横竖屏切换可以这么写:
import UIKit

//详情页1
class DetailViewController1: UIViewController {
    
    //获取 AppDelegate 对象
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //该页面显示时可以横竖屏切换
        appDelegate.interfaceOrientations = .allButUpsideDown
    }
        
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //页面退出时还原强制横屏状态
        appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
    }
}

(3)如果部分页面需要强制竖屏可以这么写:
import UIKit

//详情页2
class DetailViewController2: UIViewController {

    //获取 AppDelegate 对象
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //该页面显示时强制竖屏显示
        appDelegate.interfaceOrientations = .portrait
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //页面退出时还原强制横屏状态
        appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
    }
}
评论

全部评论(1)

回到顶部