簡體   English   中英

如何將特定View Controller的方向鎖定為縱向模式?

[英]How to lock the orientation of a specific View Controller to Portrait mode?

我希望你一切都好!

我正在使用Xcode FOR iOS 9開發Swift 2應用程序! 並且我希望整個應用程序在兩個方向上都可以旋轉,期望即使用戶旋轉,我也希望將其鎖定為縱向的特定視圖控制器。 我試了幾個小時,在網上看了很多,卻無濟於事。 有什么幫助嗎? 更新:我嘗試了以下兩種方法,但不適用於iOS 9 :(請幫助

最好的,阿納斯

目標C:

NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationValue forKey:@"orientation"];

斯威夫特:

let orientationValue = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(orientationValue, forKey: "orientation")
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

試試這個:

 override func shouldAutorotate() -> Bool {
    return onlyPortarit()
 }

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
 }

 func onlyPortarit() -> Bool{
    if(UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown){
            return true
    }else{
        return false
    }
 }

除了曼努埃爾的回應。 viewDidLoad()添加以下內容

UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

無論設備的方向如何,這都會以肖像形式初始化ViewController。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM