亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式

如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式

天涯盡頭無女友 2019-10-16 12:09:08
如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式因為我的應用程序得到了所有方向的支持。我只想鎖定特定UIViewController的縱向模式。(例如,假設它是選項卡式應用程序,并且當signin View以模態方式出現時,我只希望該signin View只有在用戶如何旋轉設備或當前設備方向的情況下才能進入縱向模式)
查看完整描述

3 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

當您有一個復雜的視圖層次結構時,事情會變得非?;靵y,比如有多個導航控制器和/或選項卡視圖控制器。

這個實現讓各個視圖控制器在希望鎖定方向時設置它,而不是依賴AppDelegate通過迭代子視圖來找到它們。

SWIFT 3&4

在附錄中:

/// set orientations you want to be allowed in this property by defaultvar orientationLock = UIInterfaceOrientationMask.allfunc a
pplication(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock}

在其他一些全局struct或Helper類中,我在這里創建了AppUtility:

struct AppUtility {

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation        }
    }

    /// OPTIONAL Added method to adjust lock and rotate to the desired orientation    
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

        self.lockOrientation(orientation)

        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }}

然后,在所需的ViewController中,您要鎖定方向:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    AppUtility.lockOrientation(.portrait)
    // Or to rotate and lock    // AppUtility.lockOrientation(.portrait, andRotateTo: .
    portrait)}override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Don't forget to reset when view is being removed    AppUtility.lockOrientation(.all)}

如果iPad或通用應用程序

確保在目標設置->General->DeploymentInfo中簽入“Required全屏”。supportedInterfaceOrientationsFor如果不選中委托,則不會調用該委托。



查看完整回答
反對 回復 2019-10-17
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

SWIFT 4

var orientationLock = UIInterfaceOrientationMask.allfunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window: 
UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock}struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation        }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
        self.lockOrientation(orientation)
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }}

您的視圖控制器如果您只需要縱向方向,請添加以下行。您必須將此應用于所有ViewController需要顯示的縱向模式。

override func viewWillAppear(_ animated: Bool) {AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo:
 UIInterfaceOrientation.portrait)
    }

這將使屏幕定位為其他視圖控制器根據設備的物理方向。

override func viewWillDisappear(_ animated: Bool) {
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)

    }



查看完整回答
反對 回復 2019-10-17
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

添加此代碼以強制顯示并鎖定它:

override func viewDidLoad() {
    super.viewDidLoad()

    // Force the device in portrait mode when the view controller gets loaded    UIDevice.currentDevice().
    setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") }override func shouldAutorotate() -> Bool {
    // Lock autorotate    return false}override func supportedInterfaceOrientations() -> Int {

    // Only allow Portrait    return Int(UIInterfaceOrientationMask.Portrait.rawValue)}
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {

    // Only allow Portrait    return UIInterfaceOrientation.Portrait}

在AppDelegate-set Support InterfaceOrientationsForWindow中,您希望整個應用程序支持任何方向:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.All}



查看完整回答
反對 回復 2019-10-17
  • 3 回答
  • 0 關注
  • 478 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號