lock orientation of a viewController in swift - ios

I'm a newbie in Swift and i have a problem locking the orientation to portrait in a viewController. Actually i have locked it using this code in my Custom Navigation Controller
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if (self.visibleViewController is ViewController)
{
return UIInterfaceOrientationMask.Portrait
}
return .All
}
Everything works fine and the ViewController is locked to portrait.The problem is when return to this controller from another in landscape mode.
if i return to ViewController (pressing back from the NextViewController) in landscape then the ViewController appeared in landscape. Any suggestion?

In swift 3 this solution works
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController?.presentedViewController is LockedViewController {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.all
}
}
Change LockedViewController to match the controller you would like locked.

You can override the methods below
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
Then, in your viewDidLoad, force the orientation by adding the code below
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")

For swift3.0
To restrict different orientations for different views You need to do following things :
In App delegate File :
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController?.presentedViewController is OtherViewController {
return UIInterfaceOrientationMask.All;
} else {
return UIInterfaceOrientationMask.Portrait;
}
}

Related

handle orientation with multiple stack of viewcontrollers

Some quick issues, i have 5 VC on stack. when i am in my second VC, then if i rotate to landscape and when i move to 1st VC my layout is totally collapsed.
in my second VC i added viewWillTransition and i am invalidateLayoutof my table view. And if i add tableview.reloaddata() in my first screen viewWillAppear .Then it's fine.
But consider if i have 5 VC on stack i can not write code in all VC. Is there any better way to handle it.
In your app delegate add this....
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
Used this from any view controller like this...
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)

How to lock orientation for iPhone for certain view controllers - Swift?

I have 2 View Controllers, VC1 and VC2.
VC1 currently presents VC2 modally.
VC1 only orientation should be portrait but VC2 can have all orientations.
The issue is when I am in VC2 and I rotate to landscape mode and then dismiss, VC1 is in landscape mode also! That should never happen!
NOTE: There is no Navigation Controller or UITabbarcontroller
I am adding my code bellow.
Appdelagate:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
}
// Only allow portrait (standard behaviour)
return .portrait;
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: (UITabBarController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
} else if (rootViewController.isKind(of:(UINavigationController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}
Code in VC2:
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
func canRotate() -> Void {}
Link to where I went for help and found this code
Website where I found Code
Thanks so much for the help!
You need to follow the below steps to lock rotation for specific ViewControllers :-
Step 1: While creating your project, allow all the orientations. Do not select anything in below image.
Step 2: If you want VC1 to have only Portrait Orientation the implementation then add the below two functions in your ViewController Class
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all //return the value as per the required orientation
}
override var shouldAutorotate: Bool {
return false
}
Step 3: If you wish VC2 to have all the orientation then do not add any code for it.
So the conclusion:-
In project setting, allow all the orientations for whole project. Restriction should be at ViewControllers level not at project level.
If you wish any VC to have all orientation then don't write any code.
If you wish any VC to have specific orientation then implement two functions above.

How to support 180 degree variant for iPad in portrait mode?

I am designing an universal app and I like to support portrait only for both iPhone and iPad. But I need iPad to support both regular and upside down portrait orientation.
So I set Supported interface orientations for both iPhone and iPad in info.plist as Portrait (top home button) and Portrait (bottom home button).
Following in AppDeleagte.swift
var shouldSupportAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.all
}
return UIInterfaceOrientationMask.portrait
}
Following in ViewController.swift
override func viewWillAppear(_ animated: Bool) {
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
let value = UIInterfaceOrientation.portraitUpsideDown.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
When I rotate my iPad upside down in simulator, it still remains upside down and does not rotate 180 degrees. Where am I going wrong?
You didn't mention how you had configured the Deployment Info for the project. By default, the 'Upside Down' option is unchecked for Universal projects, and you will need to select this to get the orientation you need
I am able to achieve this by doing
Following in AppDeleagte.swift
var shouldSupportAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.all
}
return [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
}
Following in ViewController.swift
override func viewWillAppear(_ animated: Bool) {
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
}

Changing orientations programmatically does not work fine

My whole application is in portrait mode. I just want to use one view controller in landscape mode (left).
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask
{
if let _navigationController = window?.rootViewController as? UINavigationController {
if _navigationController.topViewController is FullScreenPlayerVC {
return UIInterfaceOrientationMask.LandscapeLeft
}
}
return UIInterfaceOrientationMask.Portrait
}
This is my controller A
override func shouldAutorotate() -> Bool
{
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Portrait
}
Now i push Controller B. This is my controller B
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool
{
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Landscape
}
It works as per my requirement when i push controller B while holding my device in portrait mode but if i am holding my phone in landscape left already.
It does not perform desired action. Searched a lot about it but not able to find the solution yet.
I have tried many solutions but nothings working fine.
This is a generic solution for your problem and others related.
1. Create auxiliar class UIHelper and put on the following methods:
/**This method returns top view controller in application */
class func topViewController() -> UIViewController?
{
let helper = UIHelper()
return helper.topViewControllerWithRootViewController(rootViewController: UIApplication.shared.keyWindow?.rootViewController)
}
/**This is a recursive method to select the top View Controller in a app, either with TabBarController or not */
private func topViewControllerWithRootViewController(rootViewController:UIViewController?) -> UIViewController?
{
if(rootViewController != nil)
{
// UITabBarController
if let tabBarController = rootViewController as? UITabBarController,
let selectedViewController = tabBarController.selectedViewController {
return self.topViewControllerWithRootViewController(rootViewController: selectedViewController)
}
// UINavigationController
if let navigationController = rootViewController as? UINavigationController ,let visibleViewController = navigationController.visibleViewController {
return self.topViewControllerWithRootViewController(rootViewController: visibleViewController)
}
if ((rootViewController!.presentedViewController) != nil) {
let presentedViewController = rootViewController!.presentedViewController;
return self.topViewControllerWithRootViewController(rootViewController: presentedViewController!);
}else
{
return rootViewController
}
}
return nil
}
2. Create a Protocol with your desire behavior, for your specific case will be landscape.
protocol orientationIsOnlyLandscape {}
Nota: If you want, add it in the top of UIHelper Class.
3. Extend your View Controller
In your case:
class B_ViewController: UIViewController,orientationIsOnlyLandscape {
....
}
4. In app delegate class add this method:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
let presentedViewController = UIHelper.topViewController()
if presentedViewController is orientationIsOnlyLandscape {
return .landscape
}
return .portrait
}
Final Notes:
If you that more class are in landscape mode, just extend that
protocol.
If you want others behaviors from view controllers, create other protocols and follow the same structure.
This example solves the problem with orientations changes after push
view controllers

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.
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")

Resources