园林应用只在人像一个控制器 - 斯威夫特3

问题描述:

在应用程序委托:园林应用只在人像一个控制器 - 斯威夫特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。

写这段代码的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