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
Related
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
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.
I'm presenting a UIViewController modally with presentation style as Form Sheet, and it's not locking orientation when it appears as a form sheet. The form sheet modal presentation style looks different when shown on the iPhone 6+ in landscape, or on the iPad in any rotation.
I'm presenting the VC via a storyboard segue
This same code works correctly in locking orientation on iPhone (except 6+), but does not work when style is form sheet. How can I lock a modal form sheet in portrait on iPad or iPhone 6+?
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return .Portrait
}
this code works for me, hope it works for u too
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let val = UIInterfaceOrientation.Portrait;
UIDevice .currentDevice() .setValue(val, forKey: "orientation");
}
override func shouldAutorotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation) -> Bool {
return (toInterfaceOrientation == UIInterfaceOrientation.Portrait)
}
override func shouldAutorotate() -> Bool {
return false
}
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]
}
I have an app where all the views are portraits but only one view which is a UIWebView which loads a PDF needs to be in Landscape.
My project settings are below:
I have tried the following:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Landscape, UIInterfaceOrientationMask.LandscapeLeft]
return orientation
}
The above code does not seem to be working. I got the code by searching online. I am not sure what I am doing wrong. Any suggestions would be help and if you can provide some sample code would be great.
I just want to force landscape orientation for one UIWebView.
EDIT:
Error:
If its hard to read here is the error:
Supported orientations has no common orientation with the application, and [KM_T_World.MainViewController shouldAutorotate] is returning YES'
Implement this in your View Controller:
override func viewDidLoad() {
super.viewDidLoad()
UIViewController.attemptRotationToDeviceOrientation()
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft, UIInterfaceOrientationMask.LandscapeRight]
}
Use this code:
override func viewDidAppear(animated: Bool) {
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
And your Device orientation should be:
Result:
Sample for more Info.
UPDATE:
It is simple to lock orientation for particular view by adding this code:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask{
return UIInterfaceOrientationMask.Portrait
}