one page landscape mode swift - ios

I am trying to make only ONE view in my application on landscape mode when the device changes its position, -not from the beginning. And that the rest of the views work in portrait.
AppDelegate
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
viewDidLoad
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
This works but it applies to all my pages and I only want one in particular

In the view controller that you want to be landscape-only, override supportedInterfaceOrientations and provide an OptionSet of landscape:
public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [.landscapeLeft, .landscapeRight]
}

Related

Lock the orientation of the app in a specific View Controller in Swift

I'm working on an app that requires locking the orientation in each view controller.
I read this article, but it did not work for me.
In the project settings, I set the device orientation to Portrait, Landscape Left, and Landscape right.
I have two view controllers, VC1 and VC2, and I want to lock the VC1's orientation vertically only. I want to lock the VC2's orientation, on the other hand, horizontally only.
Then I added the following code in VC1.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
override var shouldAutorotate: Bool {
return false
}
But, I can still able to rotate the VC1 horizontally...
Side note: VC1 contains a navigation bar and a tab bar.
If you know how to solve this issue, please let me know...
You can force orientation with few steps:
Firstly, In your AppDelegate define a orientation property and conform supportedInterfaceOrientationsFor
var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
Then declare utility struct to set orientation using KVO:
struct AppOrientationUtility {
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")
}
}
How to use:
//For portrait
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
//For landscape
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)

Launching an app only in portrait orientation

I'm trying to develop an app on Swift that supports both portrait and landscape orientations but I don't want that the user gets the possibility to launch it in landscape mode.
So is there a way to prevent this opening in landscape while keeping the app's device orientation parameters on portrait, landscape left and landscape right, or is there a way to change the device orientation parameters from portrait only after the launch of the app with the UIInterfaceOrientationMask ?
Thanks a lot for your help!
Restrict it in AppDelegate's method -
var supportedOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return supportedOrientation
}
Override following in all view controllers -
override func viewDidAppear(_ animated: Bool) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
var orientation: UIInterfaceOrientationMask = .all
delegate.supportedOrientation = orientation
}
}
Try it and let me know if it works.

Lock Portrait View in App Delegate while having View controller as landscape

I need the entire app to be locked in PortraitView, except for one view Controller. I need the functionality of these functions but I get an error for having both. When I add the view controller functions app crashes. If i take away the app delegate the view controller does what I need it too.
App Delegate
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}
ViewController
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight
}
override var shouldAutorotate: Bool {
return true
}
Edited to use Base Controller View:
As I can recall if the orientation is enabled for the app. The orientation functions needs to be implemented for every controller that needs to restrict it.
Alternatively, create Base UIViewController that supports Portrait. which should be inherited by all controllers and only override the orientation functions in the landscape controller.
Something like the following:
class BaseViewController : UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .Portrait...
}
}
class ViewController1: BaseViewController {
// nothin to override
}
class LandscapeController: BaseViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight
}
}

Disable Landscape for Split View Controller for iPhone in Universal App

I have a universal app with a UISplitViewController. I want the iPad version to work in both portrait and landscape but I only want the app to work in portrait on iPhone. I tried using the following on my Main View Controller and it did not work:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
I think the problem is that the split view controller overrides the these two methods. Any ideas?
i think this is what you looking for
[splitViewController setHidesMasterViewInPortrait:NO];
I managed to figure it out by setting the supported app interface orientations at launch in the AppDelegate:
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if deviceIdiom == .Pad {
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}

Enable portrait and landscape for one view controller and only portrait for other view controllers in swift?

I have 10 view controllers in my app and i want only one view controller to be viewed in portrait and landscape mode and the rest should only be viewed in portrait mode.
Note: I am using UINavigationController.
Any help is appreciated!
I had the same problem, but found a solution:
First, allow Landscape mode for your App in the basic settings. Then modify your AppDelegate:
internal var shouldRotate = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
For every Controller, where landscape mode should be allowed, add the following code to the viewDidLoad() :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
That allows for the entire application to be shown in landscape mode. But it should be only in this controller, so add following code in viewWillDissappear() to set the shouldRotate var in the AppDelegate to false:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}

Resources