angeldev
angeldev

Reputation: 2053

Disable rotation for item in tabBarController

I have a tabBarController which has 4 items. One of them is a camera (a barcode scanner) which I implemented with AVCaptureSession. So, if you tab the tab "scanner" will automatically show you a camera screen.

The problem is that I can't disable autorotate of individual items of the tabBarController. So, the screen of the camera rotates when you rotate the device and is very weird.

I tried:

override func shouldAutorotate() -> Bool {

    return false

}

and

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    return .Portrait

}

but nothing works.

Upvotes: 0

Views: 491

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

In your AppDelegate add the following

var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.All
    }
    return UIInterfaceOrientationMask.Portrait
}

Then go to each view and add the following in viewWillAppear

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
// false = only portrait
// true = all orientations
appdelegate.shouldSupportAllOrientation = false

Update To lock the screen when you go from landscape to portrait, just add this code in viewWillAppear.

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

Upvotes: 2

Related Questions