iOS橫豎屏控制

iOS的橫豎屏配置主要由以下四部分組成

1. info.plist內(nèi)Supported interface orientations 配置應用支持的方向

截圖

2. 項目Target內(nèi)勾選應用支持的方向

截圖

3. AppDelegate內(nèi)配置Application支持的方向

    var orientationLock = UIInterfaceOrientationMask.allButUpsideDown // 默認支持方向
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock
    }

4. ViewController內(nèi)配置VC支持的方向

    open override var shouldAutorotate: Bool {
        return false
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }

如何主動控制屏幕方向

if #available(iOS 16.0, *) {
            vc.setNeedsUpdateOfSupportedInterfaceOrientations()
            // iOS 16以上需要通過scene來實現(xiàn)屏幕方向設置
            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
            var value: UIWindowScene.GeometryPreferences = .iOS(interfaceOrientations: .all)
            switch mode {
            case .port:
                value = .iOS(interfaceOrientations: .portrait)
            case .land:
                value = .iOS(interfaceOrientations: .landscape)
            case .auto:
                value = .iOS(interfaceOrientations: .all)
            }
            windowScene?.requestGeometryUpdate(value, errorHandler: { error in
                
            })
        } else {
            switch mode {
            case .port:
                /// 強制設置成豎屏
                UIDevice.current.setValue(UIInterfaceOrientation.unknown.rawValue, forKey: "orientation")
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
            case .land:
                /// 強制設置成橫屏
                UIDevice.current.setValue(UIInterfaceOrientation.unknown.rawValue, forKey: "orientation")
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
            case .auto:
                /// 設置自動旋轉(zhuǎn)
                UIDevice.current.setValue(UIInterfaceOrientation.unknown.rawValue, forKey: "orientation")
            }
        }

    /// 判斷當前屏幕是否是豎屏
    public static func isCurrentScreenPortrait() -> Bool {
        let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first ?? UIWindow()
        let orientation = window.windowScene?.interfaceOrientation
        switch orientation {
        case .portrait, .portraitUpsideDown:
            return true
        case .landscapeLeft, .landscapeRight:
            return false
        default:
            return true
        }
    }

視圖如何適應橫豎屏

        if ScreenRotationTool.isCurrentScreenPortrait() == true {
            contentView.snp.remakeConstraints { make in
                ...
            }
        } else {
            contentView.snp.remakeConstraints { make in
                ...
            }
        }

邏輯概述:
iOS按照Application->TabBarVC->NavVC->VC->ChildVC的順序來調(diào)用相關的函數(shù)或者屬性(info.plist和Target內(nèi)的配置我也不知道有什么作用),用來確定當前App支持的屏幕方向。
如果Application不支持對應方向,即使ViewController支持該方向,也是沒有作用的。

如果你的ViewController沒有重寫相關的方法,那默認就是支持所有方向的,也就不需要實現(xiàn)VC相關的方法重寫,這會節(jié)省很多代碼。

中間的坑

1. 某些第三方庫(如HBDNav),會直接重寫ViewController的相關方法,修改VC默認支持的控制器方向,如果它的默認值不是你想要的,這時候你就需要繼承來重寫相關方法了

2. 屏幕支持方向的傳遞性,App通常由UITabBarController、UINavigationController、UIViewController、ChildViewController組成,想要讓當前的VC來控制,那就需要一層層的調(diào)用下去,交由對應的VC去控制

    // TabBarController
    public override var shouldAutorotate: Bool {
        return self.selectedViewController?.shouldAutorotate ?? true
    }

    public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.selectedViewController?.supportedInterfaceOrientations ?? UIInterfaceOrientationMask.allButUpsideDown
    }
    
    public override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return self.selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }
    // NavigationController
    public override var shouldAutorotate: Bool {
        return self.topViewController?.shouldAutorotate ?? true
    }

    public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.topViewController?.supportedInterfaceOrientations ?? UIInterfaceOrientationMask.allButUpsideDown
    }
    
    public override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return self.topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }
    // ViewController(如果有childViewController,需要指定到對應的VC上)
    open override var shouldAutorotate: Bool {
        return false
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }

3. VC的supportedInterfaceOrientations系統(tǒng)不會每次都調(diào)用,要刷新需要調(diào)用vc.setNeedsUpdateOfSupportedInterfaceOrientations()

4. 跳轉(zhuǎn)VC的時候,根據(jù)下個頁面的方向,先保持一致的方向,再跳轉(zhuǎn)

其他的自己摸索下就好了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容