iPhone & iPad orientation (universal app) - iphone

I'm trying to show different orientation depending on device.
On iPhone I want to allow portrait orientation, on iPad I want to allow landscapeleft orientation.
Is that possible?
I tried
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}

The code you posted does the job. However you need to add Supported Device Orientations to Info.plist. The easiest way to do this is to select appriopriate settings in Project->Target->Summary->Supported Device Orientations section.

Add this to your app delegate: (Swift)
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue | UIInterfaceOrientationMask.LandscapeRight.rawValue)
}
}

Related

How to record video only in portrait in iOS but not in landscape?

I want to allow user to record a video only in portrait mode(orientation) and restrict the user to record in landscape. I'm using UIImagePickerController and I couldn't find any orientation options in it. could anyone help me out in this?
Write below code in AppDelegate:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
guard let mainController = navigationController else { return UIInterfaceOrientationMask.all }
if ((mainController.visibleViewController as? CameraVC) != nil) == false {
//This VC should support only Portrait Mode.
return UIInterfaceOrientationMask.portrait
} else {
//Else support All Modes.
return UIInterfaceOrientationMask.all
}
}
And It will Restrict Your "CameraVC" to Rotate in landscape Mode.

iPad orientation programmatically not working

I have an issue about the orientation on iPad.
I disabled the orientation on iPhone but i can change it programmatically on some specific controller.
But on iPad i can’t. i don’t know why although i searched in this website and some developers write answers to check UIRequiresFullScreene and i do it.
But it disables the orientation on all screens.
Is there any way to give ability to change orientation on specific controller on iPad ?
The following code i used to change iPad Orientation but it is not working.
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
Thanks advance
When you set programmatically the orientation of the device. To perform the orientation animation the function
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
from AppDelegate is being called.
If you have set it there to return only UIInterfaceOrientationMask.portrait you will never see the transition.
Also I personally have these kind of problems and to solve this I had to programmaticaly force the phone to rotate to the current Orientation and then to rotate to the wanted orientation.
Example for phone to rotate landscapeLeft from portrait
func setOrientationToLandScapeOnly() {
appDelegate.shouldForceLandscapeOnly = true
appDelegate.shouldForcePortraitOnly = false
if UIDevice.current.userInterfaceIdiom == .phone {
switch UIApplication.shared.statusBarOrientation {
case .landscapeLeft, .landscapeRight:
return
default:
APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.portrait.rawValue)
APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.landscapeLeft.rawValue)
return
}
}
}
And inside AppDelegate.swift
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if shouldForcePortraitOnly {
return UIInterfaceOrientationMask.portrait
}
if shouldForceLandscapeOnly {
return UIInterfaceOrientationMask.landscape
}
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
return UIInterfaceOrientationMask.landscape
}
return UIInterfaceOrientationMask.portrait
}

How to lock application's orientation to portrait for iPhone *only*?

I know there are some threads relating to this but I also can't figure out my specific case....
I want to lock orientation to portrait for iPhone ONLY (and allow landscape and portrait and no upside down for iPad)....
Tried some code out and pasted this in the view controller, app delegate.swift, and the tableview controller files:
I don't know why it isn't working. I'm trying to use code to apply the changes. I've read multiple threads and cannot figure out my particular case...
Thanks!
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
if UIDevice.current.userInterfaceIdiom == .phone {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.allButUpsideDown //return the value as per the required orientation
}
}
}
func supportedInterfaceOrientations(for window: UIWindow?) -> UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.allButUpsideDown //return the value as per the required orientation
}
}
var shouldAutorotateToInterfaceOrientation: Bool {
if UIDevice.current.userInterfaceIdiom == .phone {
return false
} else {
return true
}
}
override var shouldAutorotate: Bool {
if UIDevice.current.userInterfaceIdiom == .phone {
return false
} else {
return true
}
}
You don't need any code. Simply select the desired interface orientations for the Info.plist.
For your requirement you want the end result to be:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
I you are trying to set portrait mode for iPhone only for a particular controller.I f your viewController is embedded in a navigation controller in storyboard set the navigation controller delegate UINavigationControllerDelegate and add the following method
class ViewController: UIViewController, UINavigationControllerDelegate {
func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask {
if (UIDevice.current.userInterfaceIdiom == .phone){
// Ipad
return UIInterfaceOrientationMask.Portrait
}
//Ipad
return UIInterfaceOrientationMask.all //What ever condition you want to set for Ipad.
}
}

cannot get shouldautorotate to fire when changing orientation

I'm developing an app with Xcode9 using Swift4 and it was originally intended for just iPads but now needs to run on phones too. I need to lock the phones to Portrait and iPads remain Portrait/Landscape.
I've done this before using the shouldautorotate function and returning true or false depending on the device as below.
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
However, when I change the orientation it does not fire. Is this another depreciated method and if so how do I restrict the orientation now?
If your view controller is embedded into UINavigationController or UITabBarController, then this shouldAutorotate() is queried from them as this container is the topmost view controller. And after iOS updates it does not ask for contaning view controllers if they need to be rotated or not. Thus you may use your custom subclass and creat it in runtime or provide as custom calss in your storyboard:
import UIKit
class CustomNavigationController: UINavigationController {
override var shouldAutorotate: Bool {
return self.viewControllers.last?.shouldAutorotate ?? true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.viewControllers.last?.supportedInterfaceOrientations ?? .all
}
}
After much googling, this worked for me
https://stackoverflow.com/questions/38969419/ios-how-can-enable-or-disable-rotate-on-each-uiviewcontroller

Disable SplitViewController only on iPhone - Swift

I'm working on an universal iOS app and I'm using SplitViewController. I have been trying to disable the splitView on iPhone 6+ and 6s+ in landscape mode but nothing seems to work. I attempted to override the UITraitCollection by setting its horizontalSizeClass to Compact, it doesn't seem to work either. Has anyone attempted this? Below is my code to override UITraitCollection
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
let collections = [UITraitCollection(horizontalSizeClass: .Compact), UITraitCollection(verticalSizeClass: .Compact) ]
return UITraitCollection(traitsFromCollections: collections)
}else {
return super.traitCollection
}
}
I have this in my viewDidLoad of the splitViewController.