2014-05-05 63 views

回答

5

肯定的:

+0

請注意,這隻適用於'ViewController'沒有嵌入到'NavigationController'的情況,它必須以模態方式調用,因爲'NavigationController'負責自動轉換,並且不能將其更改爲只是一個'ViewController' –

+0

@B:我在我的viewController中不使用UINavigationController。 @FreeNickname:這個方法需要插入到他的rootViewController中。 –

+0

@MatteoGobbi,對,對不起。我刪除了我的評論以避免含糊不清。 – FreeNickname

0

如果你有NavigationController內的多個ViewControllers,並希望在他們一個禁用只旋轉,您需要設置和內部控制ApplicationDelegate旋轉。這裏是如何做到這一點的斯威夫特...

在AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate { 


var blockRotation: Bool = false 

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 

    if (self.blockRotation) { 
     println("supportedInterfaceOrientations - PORTRAIT") 
     return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
    } else { 
     println("supportedInterfaceOrientations - ALL") 
     return Int(UIInterfaceOrientationMask.All.rawValue) 
    } 
} 

在要阻止旋轉視圖控制器,添加UIApplicationDelegate上您的課......

class LoginViewController: UIViewController, UITextFieldDelegate, UIApplicationDelegate { 

,然後創建到AppDelegate中的參考...

var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 

在viewDidLoad中,設置appDelegate.blockRotation =真:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 

    appDelegate.blockRotation = true 

} 

在viewWillAppear中,設置取向強制器件所選擇的方向(在本例中縱向):

override func viewWillAppear(animated: Bool) { 

    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 

} 

然後,在viewWillDisappear,或在prepareForSegue,設置appDelegate.blockRotation =假:

override func viewWillDisappear(animated: Bool) { 
    appDelegate.blockRotation = false 
} 

這將一個導航CONTRO內的塊在所述一個視圖控制器旋轉包含多個ViewController的ller。希望這可以幫助。