anasganim
anasganim

Reputation: 17

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

Upvotes: 1

Views: 4333

Answers (4)

stoic0709
stoic0709

Reputation: 11

In addition to Manuel's response. Add the following in viewDidLoad()

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

This will initialize ViewController in portrait regardless of the device orientation.

Upvotes: 1

Kemo
Kemo

Reputation: 488

Objective C :

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

Swift :

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

Upvotes: 3

Manuel
Manuel

Reputation: 1695

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

Upvotes: 2

ReMeDy
ReMeDy

Reputation: 99

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

Upvotes: 2

Related Questions