I’m trying to lock the rotation of my application using a bunch of methods that apple have now depreciated over the years and I’m at a loss.
There isn’t a SceneDelegate or AppDelegate in Xcode projects anymore using SwiftUI meaning that I can’t use any of the previous methods people came up with and programming the solution in SwiftUI: How do I lock a particular View in Portrait mode whilst allowing others to change orientation?. When I try to implement this solution into my program, Xcode returns a warning BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
Here is my current code
import SwiftUI
struct ContentView: View {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some View {
ZStack {
// rest of code is here
}
}.onAppear {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation") // Forcing the rotation to portrait
AppDelegate.orientationLock = .portrait // And making sure it stays that way
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
static var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}