I want to keep support for both landscape and portrait orientation in General - > iphone orientation. However, when the app runs, I am making a start up call and based on a flag in the response , I want to disable landscape orientation or keep it throughout the app.
How can I programatically enable or disable landscape orientation?
In your view controller try overriding these properties like below:
override public var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
You may refer this apple documentation
As the requirement is to lock the application orientation based on the api response you can programatically handle it in the application delegate method
optional func application(
_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
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 have an iOS project where I want to be able to show the app only in Portrait and Upside Down mode.
I did the following:
Checked Portrait and Upside Down in target -> General -> Deployment Info -> Device Orientation
Added supportedInterfaceOrientationsForWindow function to my App Delegate file:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
Added shouldAutorotate and supportedInterfaceOrientations to my View Controller files:
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.portraitUpsideDown, UIInterfaceOrientationMask.portrait ];
}
Edited the Info.plist file to include Portrait and Upside Down:
However when I run the app on the iPhone it only shows in Portrait mode not the Upside Down mode when rotate upside down.
I am using Version 13.4.1 (13F100) on Macbook Pro and testing on an iPhone 7 with iOS 15.5.
add this in AppDelegate
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .portraitUpsideDown
}
I managed to solve it. The problem was that in the project there was a 3rd party pod named SideMenuSwift being used which had its preferences set to .portrait only. I changed it to .all and in supportedInterfaceOrientationsFor in AppDelegate I have:
return .portraitUpsideDown | .portrait
I'm working on an app that is to be in portrait mode when it is launched. After that, I add an SDK for some purpose to my project. In the process of the integrating the SDK needs both Portrait and Landscape Right checked in the General pane. So I set it as given in the SDK integration manual.
and after that when I run my project in a device and rotate it to right the app device orientation is changing. How can I set the orientation is fixed without disturbing the SDK.
if you're having trouble to set orientation right after the app launches then you might need to stop orientation for each view controller or one of it's base navigation controller
Swift 3:
class YourBaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}}
Use YourBaseNavigationController as your navigation controller.Basically all view controller within this navigation controller will be force to support portrait mode only.
unmark the checks Landscape left and landscape right
You need to disable orientation through code.
Add this code in your RootViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);//set 'return NO'; for stop orientation
}
Swift :
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
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:
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
}