Set one particular viewcontroller to landscape mode in swift - ios

How we can set one particular viewcontroller to landscape mode in swift using xib?
func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
let orientation: UIInterfaceOrientationMask =
[UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown]
return orientation
}
I am using the above code but it doesn't seem to work..

I am pretty sure that you can't be that specific for each view controller by using the storyboard. If you are doing it in code as you are, you need to specify the override keyword for that function. You also need to make sure that your app project supports all the interface orientations that you want to support for any particular view controller.

You need to check the device orientation in your proyect target
and then in all viewControllers in portrait add
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return true;
} else {
return false;
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
finally in landscape viweController you want add
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.Landscape, UIInterfaceOrientationMask.LandscapeLeft, UIInterfaceOrientationMask.LandscapeRight, UIInterfaceOrientationMask.PortraitUpsideDown]
}

Related

Change the orientation only for specific view controllers in ios swift 2.3 and swift 3

I was searching that how can I change the orientation to LandScape mode only for spefic view controller when the Device Orientation is set to Portrait in the project settings. Didn't get any succesfull result. so finally I found a way to do it.
this is project device orientation looks like ....
In swift 2.3 add following methods
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeLeft
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
For swift 3.0 do like below ....
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
Note :This will work fine, if you navigate from one view to another view controller without a navigation controller .
for more see this small video tutorial : change device orientation for specific view controller in ios
if anyone has better way to do this or comments , hope your answer or comments here . thanx .
Use supportedInterfaceOrientationsFor application delegate.add following code in AppDelegate file
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isIpadLandscape {
return UIInterfaceOrientationMask.all;
}else{
return UIInterfaceOrientationMask.portrait;
}
}
Add particular **viewcontroller** page you want to **rotate**
override func viewDidLoad() {
super.viewDidLoad()
isLandscape = true
}
override func viewWillDisappear(_ animated: Bool) {
isLandscape = false
}
And dont forget to add global varibale in appdelegate file
import UIKit
import CoreData
import Foundation
var isLandscape = false
#UIApplicationMain

Turn off rotation?

trying to turn off that user can't change orientation..
I have my view in landscape and it must be in landscaped so I'm doing it this way
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
And also with this that user can't change rotation
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeLeft
}
private func shouldAutorotate() -> Bool {
return false
}
The problem is that it isn' t working. I have after pushing landscaped but I'm still can rotate it.
From swift 3 shouldAutorotate and supportedInterfaceOrientations is property not method, so you need to add it like this.
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeLeft
}

App ignores supportedInterfaceOrientations

I have an app with several view controllers, most of which should be able to have any orientation, but one is required to be in Portrait. However, the app ignores the value I return from supportedInterfaceOrientations()
My code:
UINavigationController subclass:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if let topViewController = self.topViewController {
return topViewController.supportedInterfaceOrientations()
}
return [.Landscape, .Portrait]
}
override func shouldAutorotate() -> Bool {
if let topViewController = self.topViewController {
return topViewController.shouldAutorotate()
}
return true
}
UIViewController subclass
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
override func shouldAutorotate() -> Bool {
return true
}
When I'm in a landscape-compatible view controller and go to the one which is supposed to be in portrait, nothing happens - the app does not autorotate to portrait.
I removed supported interface orientations from info.plist as well. Any ideas?
You can set them in AppDelegate.swift, inside this function
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
return checkOrientationForVC(self.window?.rootViewController?)
}
And inside this function you can do something like :
func checkOrientationForVC(viewController:UIViewController?)-> Int{
if(viewController == nil){
return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation
}else if (viewController is SignInViewController){ //Your View controller here
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}else{
return checkOrientationForVC(viewController!.presentedViewController?)
}
}
Also set orientation in Storyboard too under "Simulated Metrics" in "Orientation".You can also see this Stackoverflow's post.

Unable to force UIViewController orientation

I have a portrait application with one landscape ViewController.
I've been digging a lot on how to force the orientation to landscape when the app is locked to portrait and I tried a lot of solutions presented here and not only but without any luck so far.
So far I managed to autorotate the VC I need in landscape but since the orientation is not locked, all other VCs will also rotate.
override func viewDidAppear(animated: Bool)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool
{
return true
}
After some more digging I ended up with this after finding a sample project but while it works in that project, it doesn't seem to work for me.
This is in AppDelegate
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController?.presentedViewController is ConclusionReferencesVC {
let conclusionReferencesVC = self.window!.rootViewController!.presentedViewController as! ConclusionReferencesVC
if conclusionReferencesVC.isPresented
{
return UIInterfaceOrientationMask.LandscapeRight;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
This is in the VC I want to have in landscape:
var isPresented = true
#IBAction
func dismiss()
{
isPresented = false
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil);
}
For some reason, the supportedInterfaceOrientationsForWindow method does not validate the initial condition.
I also tried these among others but no luck:
How to lock orientation of one view controller to portrait mode only in Swift
supportedInterfaceOrientationsForWindow in Swift 2.0
Any ideas? It seems I'm missing something but I can't figure it out what.
try this for force to LandscapeRight mode only
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
}
and then use a category like this
import UIKit
extension UINavigationController{
override public func shouldAutorotate() -> Bool
{
return (self.viewControllers.last?.shouldAutorotate())!
}
override public func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
{
return (self.viewControllers.last?.supportedInterfaceOrientations())!;
}
override public func preferredInterfaceOrientationForPresentation()-> UIInterfaceOrientation
{
return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation())!;
}
}
EDITED
If you are not using navigation controller use this
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
}
override func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
{
return .LandscapeRight;
}
I hope this helps you
As an update to Reinier Melian's post, here is the UINavigationController extension in Swift 3:
import UIKit
extension UINavigationController {
override open var shouldAutorotate: Bool {
return (self.viewControllers.last?.shouldAutorotate)!
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return (self.viewControllers.last?.supportedInterfaceOrientations)!
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation)!
}
}
Unfortunately, this code crashes if you call a UIImagePickerController, as that is contained within a UINavigationController whose last view controller is nil.
probably I´m crazy ;-), but why don´t u use this?

Upside down orientation in iPhone app using Swift

I am developing an app and intend to use upside down orientation in it. I have used the following code
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown {
self.shouldAutorotate()
}
}
But its not working. Any help!???
Device orientation (General Tab):
[x] Portrait
[x] Upside Down
[ ] Landscape Left
[ ] Landscape Right
Swift 2
// UINavigationController+UpsideDownOrientation.swift
import UIKit
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return true
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [.Portrait, .PortraitUpsideDown]
}
}
Try to put this on your plist file
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
And also verify on your target on general tab on Deployment info to have this:
On the other hand, are you using Navigation controller or Tab controller? If so, you will need to subclass navigation or tab controllers and add these two methods:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
Add this in your view controller code.
override func supportedInterfaceOrientations() -> Int{
return Int(UIInterfaceOrientationMask.All.rawValue)
}
Add this code on your Class or ViewController.
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.Portrait.rawValue
}
Add these two functions to the viewcontrollers that you want to set the orientation to. Works for Swift 3.0 because I'm using it right now.
On your project, under General, select the "Device Orientation" that you want the App to support. Then go to the .plist and edit the Supported interface orientations (iPad) for the orientation you want to support.
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight // or .landscapeLeft or .portrait or .portraitUpsideDown which ever orientation you want the viewcontroller to be
}
1) Check your info.plist for Supported interface orientations, set it as you wish
2) Check your deployment information and select your chosen device orientations

Resources