4

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:

  • When I enable iPad as a target the orientation lock works out of the box. However, I do not want to offer the app for the iPad. I just want to fix the UI for users who deliberately install iPhone apps on their iPad.
  • When I test the AppDelegate approach on an iPhone it works even if I allow all device orientations in Info.plist, so it seems to do the right thing (just not on the iPad).

I'm using Xcode 12.5 and Swift 5.

Dominik
  • 90
  • 2
  • 8
  • Take a look to my answer here https://stackoverflow.com/a/64046367/5575955 – Fabio May 16 '21 at 06:54
  • Thanks, Fabio, but I already did this. Like I wrote above, the only item I have for `UISupportedInterfaceOrientations~ipad` in my Info.plist file is `UIInterfaceOrientationPortrait`. I already deleted the other 3—and still it does not work. – Dominik May 16 '21 at 07:22
  • Do you also have the `UISupportedInterfaceOrientations` key set in info.plist (without the "~ipad")? Just a thought. – West1 May 16 '21 at 07:44
  • Yes, I got both: `UISupportedInterfaceOrientationsUIInterfaceOrientationPortrait` and `UISupportedInterfaceOrientations~ipad UIInterfaceOrientationPortrait` – Dominik May 16 '21 at 07:45

1 Answers1

5

enter image description here

Unticking Supports multiple windows solved the problem for me

i4guar
  • 599
  • 5
  • 10
  • 1
    Thanks, @i4guar, I don't understand why, but this does work, amazing! – Dominik Jun 11 '21 at 16:34
  • @Dominik this will be because to support multiple windows you have to support all device orientations (I guess so they know you're ok with random sized windows basically) – CMash Oct 17 '21 at 14:39