工程配置
1.項(xiàng)目需要支持橫豎屏她奥。需要在勾選這幾個選項(xiàng)怔蚌。
2.代碼實(shí)現(xiàn)
- 在appdelegate 中新增一個屬性
var blockRotation: UIInterfaceOrientationMask = .portrait{
didSet{
if blockRotation.contains(.portrait){
consoleLine(message: 222)
//強(qiáng)制設(shè)置成豎屏
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
is_portrait = true
}else{
consoleLine(message: 111)
//強(qiáng)制設(shè)置成橫屏
UIDevice.current.setValue( UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
is_portrait = false
}
}
}
- appdelegate中實(shí)現(xiàn) app支持的方向的方法
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
///上面聲明的屬性
return blockRotation
}
- 在相關(guān)頁面進(jìn)行調(diào)用 (我這邊與
js
交互使用的是WebViewJavascriptBridge
) 具體的用法可以百度
//MARK: - 屏幕旋轉(zhuǎn)
/// 屏幕旋轉(zhuǎn)
/// html那邊調(diào)用屏幕旋轉(zhuǎn)方法,Swift這邊獲取到傳遞的參數(shù) 做相應(yīng)的處理
/// - Parameter jsonValue: <#jsonValue description#>
func screenChange(_ jsonValue:JSON){
let orientation = jsonValue["orientation"].intValue
if orientation == 1 { /// 豎屏
kAppdelegate?.blockRotation = .portrait
}else{ ///橫屏
//進(jìn)入下一頁面功咒,轉(zhuǎn)換為橫屏
let rotation : UIInterfaceOrientationMask = [.landscapeLeft, .landscapeRight]
kAppdelegate?.blockRotation = rotation
}
}
- 在控制器中實(shí)現(xiàn)愉阎。
override func viewDidLoad() {
super.viewDidLoad()
myWebView.initData()
view.addSubview(myWebView)
///初始的時候界面布局
myWebView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(StatusBar_Height)
make.right.bottom.left.equalToSuperview()
}
UIDevice.current.beginGeneratingDeviceOrientationNotifications() //開始注冊屏幕方向轉(zhuǎn)換通知
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(dingLogin_CallBack(_:)), name: NSNotification.Name(rawValue: ding_login_Success_noti), object: nil) ///釘釘?shù)卿洷O(jiān)聽
// Do any additional setup after loading the view.
}
@objc func deviceOrientationDidChange() {
///調(diào)整界面布局
if UIDevice.current.orientation == .portrait{ ///豎屏
myWebView.snp.remakeConstraints { (make) in ///重做約束
make.top.equalToSuperview().offset(StatusBar_Height)
make.left.right.equalToSuperview()
make.bottom.equalTo(view.snp.bottom)
}
}else{ ///橫屏狀態(tài)
print("UIDevice.current.isX() == \(UIDevice.current.isX())")
myWebView.snp.remakeConstraints { (make) in
make.top.equalToSuperview().offset(StatusBar_Height)
make.bottom.equalTo(view.snp.bottom)
make.left.equalToSuperview()
make.right.equalToSuperview().offset(-safeAreaHeight)
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
UIDevice.current.endGeneratingDeviceOrientationNotifications() /// 結(jié)束通知
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
setStatusBarHidden(false) //iphoneX 系列橫屏后會將狀態(tài)欄隱藏绞蹦。所以這里主動調(diào)用下。
}
func setStatusBarHidden(_ hidden:Bool){
let statusBar:UIView = (UIApplication.shared.value(forKey: "statusBarWindow") as! UIView).value(forKey: "statusBar") as! UIView
statusBar.isHidden = hidden
}
override var prefersStatusBarHidden: Bool {
return false
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
view.frame = UIScreen.main.bounds
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
- 常用常量定義
// 狀態(tài)欄高度
let StatusBar_Height = UIApplication.shared.statusBarFrame.height
///適配iPhoneX
let distanceTop:CGFloat = (UIDevice.current.isX() ? 88 : 64)
let distanceBottom:CGFloat = (UIDevice.current.isX() ? 34 : 0)
let navHeight:CGFloat = 44
///頂部高度
let app_TopHeight = StatusBar_Height + navHeight
///iPhoneX安全區(qū)域高度
let safeAreaHeight:CGFloat = StatusBar_Height > 20 ? 34 : 0
/// 底部高度
let app_BottomHeight = 49 + safeAreaHeight
//MARK: - 判斷機(jī)型是否是 iPhoneX
/**
* -- 不能用屏幕高度去判斷榜旦。因?yàn)閍pp內(nèi)部有涉及到橫屏
*/
extension UIDevice {
func isX()->Bool {
if #available(iOS 11, *) {
if let w = UIApplication.shared.delegate?.window,
let window = w, window.safeAreaInsets.left > 0 || window.safeAreaInsets.bottom > 0 {
return true
}
}
return false
}
}
/**
* -- 設(shè)備相關(guān)
*/
let kAppdelegate = UIApplication.shared.delegate as? AppDelegate