anasganim February 2016

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

I hope you are well!

I am working on a Swift 2 app using Xcode FOR iOS 9! and I would like the whole app to rotate on both directions expect a particular view controller that I want to be locked to portrait even when the user rotates. I tried for hours and looked a lot online and nothing worked. Any help? UPDATE: I tried the two methods below but it doesn't work for iOS 9 :( Please help

Best, Anas

Answers


Kemo February 2016

Objective C :

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

Swift :

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


S.Ranjbar February 2016

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


Manuel February 2016

Try with this:

 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
    }
 }

Post Status

Asked in February 2016
Viewed 3,652 times
Voted 13
Answered 3 times

Search




Leave an answer