【Swift5.1】屏幕旋转+ 状态栏的隐藏

  • 之前一直想研究一下屏幕的旋转问题,看到网上的例子太多不太统一,所以今天研究了一下话不多说直接上代码
  • 首先设置一下AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate {


    var blockRotation : UIInterfaceOrientationMask = .portrait{
        didSet{
            if blockRotation.contains(.portrait){
                //强制设置成竖屏
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
            }else{
                //强制设置成横屏
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
            }
        }
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        
        print("AppDelegate ---supportedInterfaceOrientationsFor")
        
        return blockRotation
    }
}
  • 然后在自己需要旋转的ViewController中做如下操作,我这个例子是点击的按钮的时候才旋转
import UIKit

class InterFaceOrientationViewController: UIViewController {

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 这里是一开始到这个界面就开始旋转屏幕
//        let kAppdelegate : AppDelegate? = UIApplication.shared.delegate as? AppDelegate
//        kAppdelegate?.blockRotation = UIInterfaceOrientationMask.landscapeRight
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.title = "详情"
        
        let btn = UIButton(type: .custom)
        btn.backgroundColor = UIColor.red
        btn.setTitle("旋转屏幕", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(actionBtn(sender:)), for: .touchUpInside)
        self.view.addSubview(btn)
        btn.snp.makeConstraints { make in
            make.center.equalTo(self.view.snp.center)
        }
        
        
    }
    
    @objc func actionBtn(sender:UIButton){
        sender.isSelected = !sender.isSelected
        let kAppdelegate : AppDelegate? = UIApplication.shared.delegate as? AppDelegate
        if(sender.isSelected){
            kAppdelegate?.blockRotation = UIInterfaceOrientationMask.landscapeRight
        }else{
            
            kAppdelegate?.blockRotation = .portrait
        }
        
    }
    

    override var prefersStatusBarHidden: Bool {
        return false
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return UIStatusBarStyle.lightContent
    }
    

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        let kAppdelegate : AppDelegate? = UIApplication.shared.delegate as? AppDelegate
        kAppdelegate?.blockRotation = UIInterfaceOrientationMask.portrait
    }
    
    
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        print("屏幕旋转")
    }
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

  • 到这屏幕旋转的例子就结束了
  • 下面讲一下状态栏的显示与隐藏和状态栏的style修改
  • 在网上找的例子都是在 需要隐藏和修改style的ViewController 重写这两个方法:

    override var prefersStatusBarHidden: Bool {
        return false
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return UIStatusBarStyle.lightContent
    }
  • 但是在实际的开发过程中,很少应用单个的ViewController作为开发,大多数都是使用UINavigationController + UIViewController 这种模式开发,所以光在ViewController 中重写上面的两个方法是不够的,需要自定义一个 UINavigationController 在其内部重写这两个方法:

class MainNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        // Do any additional setup after loading the view.
    }
    
    
    override var prefersStatusBarHidden: Bool{
        return self.topViewController?.prefersStatusBarHidden ?? false
    }
    
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return self.topViewController?.preferredStatusBarStyle ?? UIStatusBarStyle.default
    }

}
  • 然后加上上述中ViewController中的两个方法就能实现状态栏的显示与隐藏和style的变化
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值