美文网首页
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]终极横竖屏切换解决方案

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

  • JS 与 IOS 交互-横竖屏切换

    IOS 设备横竖屏情况 一般情形 所有界面都支持横竖屏切换如果App的所有切面都要支持横竖屏的切换,那只需要勾选【...

  • iOS 16强制切换横竖屏失效解决

    ios16切换横竖屏代码 注意:1.ios16 开始 UIDeviceOrientationDidChangeNo...

  • 跳转自动横屏

    iOS 知识小集(横竖屏切换) 转载自 http://www.cocoachina.com/ios/2016072...

  • IOS 横竖屏

    //iOS规定不允许强制用代码切换横竖屏 if([[UIDevicecurrentDevice]respondsT...

  • iOS16适配:APP切换横竖屏问题

    iOS16之前切换横竖屏使用的是UIDevice的setValue:forKey:方法进行切换。但在iOS16后不...

  • iOS 横竖屏切换

    AppDelegate.h @property(nonatomic,assign)BOOL isAllowRota...

  • iOS 横竖屏切换

    1、打开横竖屏开关 2、因为屏幕翻转后width和height是相反的 github地址:https://gith...

  • iOS 横竖屏切换

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...

  • iOS 横竖屏切换

    本文只针对单个界面横竖屏界面 1.首先在TARGETS中将横竖屏勾选上(不用勾选貌似也可以,只不过需要在AppDe...

网友评论

      本文标题:iOS 横竖屏切换

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