Lock screen Orientation to portrait - ios swift - ios

I am creating an application with support only for portrait mode. I don't want Landscape or Portrait upside down. I tried some code. Which allows me to lock in portrait mode.
I am using navigationcontroller and presentviewcontroller. Now my problem is:
1. If I rotate my device upside down and open my application it opens in upside down mode which is wrong.
2. I click some button and enter to presentviewcontroller it returns to portrait mode.
I want all the navigationcontroller and presentviewcontroller in portrait mode
My codes:
I set device orientarion portrait in Target -> General -> Deployment Info -> Portrait
In my appdelagate.swift:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
In my First view controller. Which is basically child of Navigation Controller
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ]
}
Edit 1:
Also I set StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait
Edit 2:
My settings file

Go To Target --> General and set Orientation Mode to Portrait.
Also check info.plist. Make sure Supported Interface Orientations contains only one value(Portrait). Sometime it removes from settings but not updated in plist file.

If your app is not supporting split screen view than you should check the option to Require full screen. This fixed my issue with portrait mode.
If this does not resolve issue add Supported interface orientations Key of Array type in your info.plist. And String item to that array with value Portrait (bottom home button)

For me the only working solution was to add:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
.portrait
}
in the app's delegate class, as indicated in the question itself.
I'm using SwiftUI without any storyboard file, even for the splashscreen.

Related

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.

Can't force rotate of orientation in landscape mode

My entire app is portrait mode. However, one screen needs to be able to rotate to landscape (for playing video). I am able to enable all orientation rotations with no problem via #Jonathan Danek's answer.
In my AppDelegate I have:
static var orientationLock = UIInterfaceOrientationMask.portrait
and subsequently:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
So to enable rotation for all orientations, I do the following as the video view-controller is presented:
AppDelegate.orientationLock = UIInterfaceOrientationMask.all
UINavigationController.attemptRotationToDeviceOrientation()
This works great šŸ‘ ..the problem is when I try to change it back to only portrait mode.
I am trying this as the video view-controller is dismissed:
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UINavigationController.attemptRotationToDeviceOrientation()
..but it does not trigger the above AppDelegate's supportedInterfaceOrientationsForWindow function if the video view-controller is currently rotated to Landscape. What's interesting is that it will trigger the function if the video view-controller is currently rotated to Portrait.
So I'm wondering how I can set my one view-controller to have all orientations, but then change it back to just portrait?
Have you tried to overwrite supportedInterfaceOrientations in your controller? That might help.
Example:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
Returns all of the interface orientations that the view controller supports.
Apple Doc - supportedInterfaceOrientations
Swift 5.1 short version:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
.all
}

Portrait mode all views except one view to be rotated programatically to landscape under a UINavigationController

First of all, my project hierarchy:
tab bar controller -> navigation master -> view controllers
I have one page I want to rotate in landscape mode.
My project is compatible with all the device orientation modes.
I've manually frozen all the view controllers with autorotation returning false:
override func shouldAutorotate() -> Bool {
return false
}
except the one I want to rotate. S
I'm trying this in the view controller I want to be displayed in landscape:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
But does nothing.
Also, I experimented an issue with this method. When the app was starting in landscape, the view was displaying in landscape and was unable to rotate. I manage to fix it with this method In my app delegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if(self.restrictRotation == false){
return UIInterfaceOrientationMask.Portrait//UIInterfaceOrientationMaskPortrait
}
else{
return UIInterfaceOrientationMask.All//UIInterfaceOrientationMaskAll
}
}
And in viewDidLoad setting the var to true where I want to allow rotations:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.restrictRotation = true
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
I've tested with the debugger and the method application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) is called, but the app don't rotate. I guess is because with a tab bar and a navigation controller, things has to be done differently, but don't know how exactly. Can anyone help with this?

iOS app can support landscape orientation for only one tabBar item?

Iā€™m making iOS application using swift. My app has many view and all view has UITabBar as root view. I need to support landscape orientation for only one tabbar item.
tabBar 1 - Portrait only
tabBar 2 - Portrait only
tabBar 3 - Portrait only
tabBar 4 - Landscape & Portrait
How can i do that?
I have a working solution based on this answer here: https://stackoverflow.com/a/24928474/1359306
So first off, make sure the supported orientations are checked in Target Settings > General > Deployment Info.
Add this code (taken from the linked answer) to your app delegate:
internal var shouldRotate = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if shouldRotate {
return .AllButUpsideDown
} else {
return .Portrait
}
}
Then in your landscape only view controller, put the following code in viewDidAppear (NOT viewDidLoad!) to enable both portrait and landscape view:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.shouldRotate = true
And the opposite to viewWillDisappear to disable landscape again.
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.shouldRotate = false
I am also hiding my tabs and status bar when the app is in landscape, which feels like a much better UX than tapping a tab and automatically rotating the screen.
There are a few things which in my case I don't have to worry about.
Displaying a modal view from a landscape view would cause issues

Orientation issue in iPad with iOS 8

I am facing an orientation issue in iOS 8 and later in iPad. My application requires both landscape right and landscape left orientations in iPad. But it supports only landscape right orientation in iPad with iOS 8. If you try to change the orientation , the orientation of status bar is getting changed, but the application's view controller orientation remains unchanged. I am using Xcode 6.2 and swift. I have added all orientations in plist and used func shouldAutorotate() -> Bool{}
func supportedInterfaceOrientations() -> Int {} methods as per the requirement. Please give me a solution. Thanks in advance.
I wanted to have only portrait in iphone and all modes in iPad. Overriding viewcontroller shouldrotate didn't help me out. With research found that , appdelegate can handle the rotation. Please find the below code.Hope it helps you.
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
var orientation = Int(UIInterfaceOrientationMask.Portrait.toRaw())
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
orientation = Int(UIInterfaceOrientationMask.All.toRaw())
}
return orientation
}

Resources