How can I enable rotation of screen programmatically in Swift? - ios

I am have 3 VC and in Xcode I enabled only Portrait mode but for one VC need enable Landscape Left & Right mode.
How I can enabled these orientation programmatically?
I am try use this code but it's don't help me:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .all
}
override open var shouldAutorotate: Bool {
return true
}
Also did try use next way:
Enable all screen orientation on xcode, and set for two VC only portrait mode, it's not bad BUT my VC have few subviews and when I hold phone in landscape orientation and then start program, VC have portrait orientation but other subview which I am create PROGRAMMATICALLY change self position on screen.
How I am can solve this problem?

Related

How to allow only single UIViewController to rotate upside down with Xcode?

In my app all screens have portrait orientation. How to allow only single UIViewController be in two orientation mode: portrait and upside down? When user rotates an iPhone, the UIViewController should rotates too.
First of all, In your application all screens except one screen are in portrait orientation. So You cannot set orientation of your application as portrait. So Set Device Orientation as portrait and LandScape both.
Put following code on which screen you need landscape orientation in ViewDidLoad
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
and
override var shouldAutorotate: Bool {
return true
}
Put following code on all screens where you need only portrait orientation in viewDidLoad
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
If you have any function that continuously called then you can check the device orientation inside this function as-
if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
// set your view constraints
}
if UIDevice.current.orientation == UIDeviceOrientation.portrait {
// set your view constraints
}
Otherwise you should create a monitoring function and check device orientation inside the function.

Constraints are not working when changing view orientation

How to force update constraints to position view correctly
My app supports only Portrait orientation, but one view controller should always open in landscape mode. I managed to change orientation forcefully and working properly.
But my view constraints are not working properly when navigating to that view for the very first time or navigate after changing orientation of device. Once it loaded and no orientation change happen its view loaded as expected. Constraints are not framing sub view properly in orientation changed when device is already in that orientation. I am working in xcode 8.2.1.
I checked many links but didn't find any solution for this problem. I tried to update view and constraints also.
self.view.setNeedsUpdateConstraints()
self.view.setNeedsLayout()
self.view.setNeedsDisplay()
self.view.setNeedsFocusUpdate()
Code I am using is -
override func viewDidLoad()
{
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewWillDisappear(_ animated: Bool)
{
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var shouldAutorotate: Bool
{
return true
}
View loaded as this when my device is in already in landscape mode and i navigate to this view controller and force for landscape mode. It updated to landscape mode but don't update constraints.
When press back and again select the option it shows as it should be in landscape mode.

iOS How to replicate Youtube App player rotation?

Any idea about how the feature I show in this video is done?
It looks like the touch on the "expand" button force an orientation change for the device (infact when I swipe I'm pulling the control centre out).
Anyway only the video player seems to rotate. the scrollview underneath keeps its portrait orientation.
Looks like they're presenting the player controller with a custom segue animation, while having the player controller supporting only landscape.
Try presenting a view controller that has these implemented, like so:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
override func shouldAutorotate() -> Bool {
return false
}
Then the device should be in landscape. All you have left is to do the custom transition animation between the two and you're done.
From the docs:
preferredInterfaceOrientationForPresentation
If your view controller implements this method, your view controller’s
view is shown in the preferred orientation

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

Force Landscape orientation, Swift

I have a view controller that is designed in portrait view. I have another view controller designed in Landscape view (in storyboards). On my first view controller (portrait VC) i have a button that takes you to the landscape VC. When you click on the button the Landscape view controller shows up in portrait view initially and doesn't change until rotated. I would like to have this landscape view controller automatically show up in landscape without having to rotate it.
Ive been coding this in swift, and have had no luck. In my plist menus i have all orientations enabled.
Any help would be great.
You should add this in the second viewcontroller (designed to be in landscape) in the viewDidLoad
It is in swift3 :
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
Hope it will help you :)
I believe the best and most robust way to achieve this is by overriding two attributes - and avoid force orientation changes of the windows (as suggested by others).
This is how I got one of my view controllers to always show up in landscape mode, and stay there, without affecting the entire navigation stack. Add both of these overrides to the landscape view controller:
override var shouldAutorotate: Bool {
false
}
This will prevent the view controller from auto-rotating when device orientation changes. Now that the view controller no longer rotates by itself, all you need to do is request the orientation you want:
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
UIInterfaceOrientation.landscapeLeft
}
And voila! Should do what you want.
I'm working on a similar scenario I want to load and keep my first ViewController in Landscape orientation while allowing subviews to switch as needed
I think placing
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
should load your view in landscape mode...But if holding phone in portrait orientation it will soon rotate...and that's the part I'm at...How to prevent the rotation.

Resources