- 之前一直想研究一下屏幕的旋转问题,看到网上的例子太多不太统一,所以今天研究了一下话不多说直接上代码
- 首先设置一下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的变化