2017-03-07 18 views
0

在應用程序委託:園林應用只在人像一個控制器 - 斯威夫特3

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscape.rawValue) 
} 

在我的視圖控制器(MainViewController)我已經加入

override func viewDidLoad() { 
    super.viewDidLoad() 

    let value = UIInterfaceOrientation.portrait.rawValue 
    UIDevice.current.setValue(value, forKey: "orientation") 
    supportedInterfaceOrientations() 
    preferredInterfaceOrientationForPresentation() 
    // Do any additional setup after loading the view. 
} 

private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue) 
} 

private func shouldAutorotate() -> Bool { 
    return true 
} 

private func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { 

    // Only allow Portrait 
    return UIInterfaceOrientation.portrait 
} 

這是應用程序的唯一控制器我想在肖像模式下工作。除了這一切在景觀模式。

但我已經嘗試了很多事情,仍然無法理解爲什麼它不工作。

在此先感謝。對不起,快速成爲noob。

回答

1

寫這段代碼的appdelegate

var shouldRotate = false 
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if shouldRotate { 
     return .landscape 
    } 
    else { 
     return .portrait 
    } 
} 

組驗證碼到您的視圖控制器的viewDidLoad()

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    appDelegate.shouldRotate = true // or false to disable rotation 
    //you can manage only changing true or false 

希望它可以幫助你

+0

嘿@seggy沒有它沒有工作。它確實將鍵盤的佈局更改爲肖像。但視圖控制器仍然是風景。 – Prateekro

+0

夥計在我的項目上正常工作。我認爲你是錯的。 – seggy

+0

嘿@seggy當我旋轉設備時,它不起作用,不要強迫改變方向。我想要控制器強制更改方向 – Prateekro