美文网首页
iOS 横竖屏切换

iOS 横竖屏切换

作者: 怀可 | 来源:发表于2020-03-25 17:36 被阅读0次

    所有 ViewController 都要继承自中间类,包括 TabBarController 和 NavigationController,当有这两者时,其 ChildViewController 的方向跟随 TabBarController 和 NavigationController。重写的方法直接 return topViewController 的对应值

    Navigation

    class BaseNavigationController: UINavigationController {
      
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return topViewController?.supportedInterfaceOrientations ?? self.supportedInterfaceOrientations
        }
        
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return topViewController?.preferredInterfaceOrientationForPresentation ?? self.preferredInterfaceOrientationForPresentation
        }
    }
    

    普通界面的基类返回当前 APP 的主要方向(一般为竖屏)

    class BaseViewController: UIViewController {
    
        override var shouldAutorotate: Bool {
            return true
        }
        
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .portrait
        }
        
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .portrait
        }
    }
    

    TabBar 返回自己的方向,避免在 TabBar 旋转

    class HomeTabBarController: RAMAnimatedTabBarController {
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .portrait
        }
        
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .portrait
        }
    }
    

    第一个 ViewController,不用加关于旋转屏的代码

    class ViewController: BaseViewController {
        
        @objc func pushTUI() {
            self.navigationController?.pushViewController(ToViewController(), animated: true)
        }
        
        @objc func presentTUI() {
            let vc = ToViewController()
            vc.modalPresentationStyle = .overFullScreen
            self.present(vc, animated: true, completion: nil)
        }
        // MARK: - Life Cycle
        override func viewDidLoad() {
            super.viewDidLoad()
            commonLoad()
        }
        
        private func commonLoad() {
            view.addSubview(pushBtn)
            view.addSubview(presentBtn)
        }
    
        private lazy var pushBtn: UIButton = {
            let btn = UIButton(frame: CGRect(x: 60, y: 200, width: 80, height: 80))
            btn.backgroundColor = UIColor.blue
            btn.setTitle("Push", for: .normal)
            btn.addTarget(self, action: #selector(pushTUI), for: .touchUpInside)
            return btn
        }()
            
        private lazy var presentBtn: UIButton = {
            let btn = UIButton(frame: CGRect(x: 60, y: 300, width: 80, height: 80))
            btn.backgroundColor = UIColor.red
            btn.setTitle("Present", for: .normal)
            btn.addTarget(self, action: #selector(presentTUI), for: .touchUpInside)
            return btn
        }()
    }
    

    需要强制横屏后进入的 ViewController

    class ToViewController: BaseViewController {
        
        // MARK: - 旋转屏
        override var prefersStatusBarHidden: Bool {
            // 隐藏状态栏
            return false
        }
        
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .landscapeRight
        }
        
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .landscapeRight
        }
        
        // MARK: - 侧滑手势(Push 的需要加)
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        }
        
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        }
        
        override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
            // Push 的需要加
            UIDevice.current.setValue(NSNumber(value: UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
            super.dismiss(animated: flag, completion: completion)
        }
        
        @objc func dismissTUI() {
            self.dismiss(animated: true, completion: nil)
        }
        
        // MARK: - Life Cycle
        override func viewDidLoad() {
            super.viewDidLoad()
            commonLoad()
        }
        
        private func commonLoad() {
            // Push 的需要加
            UIDevice.current.setValue(NSNumber(value: UIInterfaceOrientation.landscapeRight.rawValue), forKey: "orientation")
            if presentingViewController != nil {
                view.backgroundColor = .red
            } else {
                view.backgroundColor = .blue
    
            }
            view.addSubview(dismissBtn)
        }
        
        private lazy var dismissBtn: UIButton = {
            let btn = UIButton(frame: CGRect(x: 60, y: 200, width: 80, height: 80))
            btn.backgroundColor = .black
            btn.setTitle("dismiss", for: .normal)
            btn.addTarget(self, action: #selector(dismissTUI), for: .touchUpInside)
            return btn
        }()
    }
    

    Tips

    iOS 13 开始 present ViewController 时需要设置一下 modalPresentationStyle, 要在创建的时候设置,不要在viewDidLoad里设置

    @objc func presentTUI() {
        let vc = ToViewController()
        vc.modalPresentationStyle = .overFullScreen
        self.present(vc, animated: true, completion: nil)
    }
    

    相关文章

      网友评论

          本文标题:iOS 横竖屏切换

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