I have a SwiftUI project. For all but one of the views, I want to allow portrait and only portrait mode. For only one view, I want to allow both portrait and landscape. There are some resources on Swift but I couldn't find any on SwiftUI.
Did anyone find a way to accomplish this?
I had to do something similar to this. Here's our approach.
We set the project orientation to only support Portrait mode.
Then in your AppDelegate
add an instance variable for orientation and conform to the supportedInterfaceOrientationsFor
delegate method.
static var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
Then when your about to present your landscape view perform the following actions:
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
And on dismissal,
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
Hope this helps!
A couple of tweaks to the answer above:
In AppDelegate as Jonathan's answer above:
static var orientationLock =
UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window:
UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
Then in the 'destinationView' - the one that is landscape:
import SwiftUI
struct DestinationView: View {
var body: some View {
Group {
Text("Hello")
}.onAppear {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
.onDisappear {
DispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
}
}
Note the .rawValue in the UIDevice line which gets rid of the 'LongValue' error. Also, in the .onDisappear I had to use DispatchQueue.main.async in order to avoid an error when returning back to previous view which is in portrait.
If your app is using SwiftUI lifeCycle, the app is launched using a custom struct that conforms to the App protocol. Your app doesn't have AppDelegate which is needed to implement the above solutions.
To make your own AppDelegate try this code in your appnameApp.swift
@main
struct appnameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
static var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// something to do
return true
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window:UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}
Source: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With