I'm really new to IOS develop and before ask this question I already tried literally all the answer on stackoverflow but couldn't find anything that work (probably I fail somewhere).
I want to Disable the possibility of rotate the device view in all my App and I want to decide in every ControllerView if use portrait or landscape.
How can I do that with Swift?
you should try this on every ViewController an set it with the possibility to rotate, and what orientation should support.
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
I got this error:
Related
XCode: 14
iOS: 16 (supports upto ios12)
i am writing ios sdk, which presents some UI when we call its method. but since its an SDK, i don't have access of client app delegate.
Goal: there are 2 screens (A) and (B). if screen(A) is on let say portrait mode, and user go to screen(B) from screen(A) then even if user rotate device to any other orientation, it should not rotate screen(B).
SDK supports min version ios 12 to 16+.
tried a few methods but none of them worked.
that's why posted a question here.
Shouldautoroate(),
Preferred Orientation () doesn't work.
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override open var shouldAutorotate: Bool {
return false
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
tried above approach but doesn't work.
shows erorr
BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
Update 1
i am able to implement orientation lock on screen, but its like, it will rotate it for a second, and figure out if it matches or supported orientation or not, if it doesn't match then it will rotate to require orientation.
but all of this takes 1-2 seconds, but i want to lock is completely, in sense that it should not even rotate for a second.
Update 2
i am able to implement lock orientation feature in iOS SDK. but that requires an additinal call. i am not sure if its a best way.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
// here .all - indicates current client app supports all orientations.
return <SDKClassName>.supportedInterfaceOrientations(.all)
}
supportedInterfaceOrientations() method check if current top viewcontroller is of kind SDKViewController and also checks for current interface orientation and update it to either landscape or portrait depending upon value, if top view controller is not SDKViewController then it returns the original supported interface mask value.
looking for a better solution now.
Thanks.
If you want to present different UIs for different orientations, for what I understood. The best API IMO is:
https://developer.apple.com/documentation/uikit/uiwindowscene/3198088-interfaceorientation
Import UIKit // guess is necessary
// inside your function or class check
UIWindowScene.interfaceOrientation (...)
//Then you have three options to check your environment
// Getting the interface attributes
var traitCollection: UITraitCollection
// The traits that describe the current environment of the scene.
var coordinateSpace: UICoordinateSpace
// The coordinate space occupied by the scene.
var sizeRestrictions: UISceneSizeRestrictions?
from those you can create one UI or another.
I think I had the same issue in the past and the problem was ViewController was put into NavigationController.
If so - subclass NavigationController, override same methods that you did in ViewController B, and use it for wrapping ViewController B
class CustomNavigationController: NavigationController {
override open var shouldAutorotate: Bool {
return false
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
}
I want to change the orientation of my app. The only thing I found is:
self.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT)
but it says the UIA_Device_Orientation_Portrait could not be found.
Can anyone tell me why (perhaps I have to import something) or how else i could set the Orientation to Portrai mode?
thanks
If you wanted to rotate manually, do like this:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")
If automatically, do like this:
// ViewController.swift
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .All
}
And If your view controller is in a navigation/tabbar controller, please set that navigation/tabbar controller.
UPDATE:
This is how to control autorotate
override func shouldAutorotate() -> Bool {
return false // adjust this
}
Make sure you have your main plist adjusted to allow multiple orientations. You'll find this under the Supported interface orientations tag in your plist.
I want to Restrict one page in portrait orientation while whole application in both landscape and portrait orientation how to achieve this functionality. I have tried old code from stack over flow but it's not working.
If any one solved such problem then please help me thanks in advance.
Add this snippet to the ViewController which should not rotate:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
I'm making an app which supports landscape orientations only when playing a video. Otherwise, all the scenes support only portrait orientation. I've checked portrait, landscape left and right in project settings. I've written the following code in the ViewControllers where I want to restrict to only portrait.
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.Portrait.rawValue
}
Still, when I press (Command + right or left arrow), the simulator rotates. I don't have a device, so I have to test it in simulator.
Please help! Thank you!
It's the parent navigation controller that decide if its content should rotate or note.
You will need to override UINavigationController and had something like this
override func shouldAutorotate() -> Bool {
return self.topViewController.shouldAutorotae()
}
override func supportedInterfaceOrientations() -> Int {
return self.topViewController.shouldAutorotae()
}
Two things to check. First, check your info.plist file and make sure that you have portrait deleted for both iPhone and iPad. I was having the same issue and it was because I hadn't deleted the iPad "Portrait" options. See here:
Second, the below code can help. However, it may not work if you have a navigation controller.
override var shouldAutorotate: Bool {
return true
}
I want to lock the device's orientation in a specific ViewController to only portrait but none of the available solutions are working. It is embedded in a UINavigationController.
I deployed it to iOS7 and in the general settings, both landscape and portrait are checked because I want it to rotate for other VCs other than the specific one.
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
public override func supportedInterfaceOrientations() -> Int {
return visibleViewController.supportedInterfaceOrientations()
}
}
this portion of the code is in my ViewController class
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientation.Portrait.rawValue)
}
"In iOS 8, all rotation-related methods are deprecated" (See documentation on UIViewController). This could be why it isn't working for you.
I am thinking that Apple has done this to keep the users' experience more consistent throughout the app.
I figured out the problem which is since I am using SWRevealViewController, the parent VC overrides the subview's rotate settings.
Thus I just subclass it and adjust the autorotate and orientation settings as intended.