Dominik
Dominik

Reputation: 90

How to lock orientation in portrait for an iPhone SwiftUI app running on iPad?

I have a SwiftUI iPhone-only app with its orientation locked in portrait, which works great. However, when the same app is run on an iPad (in emulation mode; iPad is not a target) the lock has no effect: the UI rotates with the device, which is unintended as it breaks the UI. It fails in the simulator and on an actual device.

I can reproduce this issue using an empty SwiftUI project with only a single view:

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
    }
}

iPhone works as expected:

iPhone in portrait mode iPhone in landscape mode

iPad does not respect orientation lock:

iPad in portrait mode iPad in landscape mode

I have the following in my Info.plist, but no success. The UI still rotates on the iPad.

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

Then I defined an AppDelegate. Still no success, the UI keeps rotating on the iPad.

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
}

@main
struct orientationApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

What is interesting:

I'm using Xcode 12.5 and Swift 5.

Upvotes: 4

Views: 4064

Answers (1)

i4guar
i4guar

Reputation: 677

enter image description here

Unticking Supports multiple windows solved the problem for me

Upvotes: 5

Related Questions