title: How to lock orientation in portrait for an iPhone SwiftUI app running on iPad?
body: <p>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.</p>
  <p>I can reproduce this issue using an empty SwiftUI project with only a single view:</p>
  <pre><code>struct ContentView: View {
      var body: some View {
          Text(&quot;Hello, world!&quot;)
      }
  }
  </code></pre>
  <p><strong>iPhone works as expected:</strong></p>
  <p><a href="https://i.stack.imgur.com/8eevLm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8eevLm.png" alt="iPhone in portrait mode" /></a>
  <a href="https://i.stack.imgur.com/qN1ebm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qN1ebm.png" alt="iPhone in landscape mode" /></a></p>
  <p><strong>iPad does not respect orientation lock:</strong></p>
  <p><a href="https://i.stack.imgur.com/mJEPYm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mJEPYm.png" alt="iPad in portrait mode" /></a>
  <a href="https://i.stack.imgur.com/UoCWhm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UoCWhm.png" alt="iPad in landscape mode" /></a></p>
  <p>I have the following in my Info.plist, but no success. The UI still rotates on the iPad.</p>
  <pre><code>&lt;key&gt;UISupportedInterfaceOrientations&lt;/key&gt;
  &lt;array&gt;
      &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
  &lt;/array&gt;
  &lt;key&gt;UISupportedInterfaceOrientations~ipad&lt;/key&gt;
  &lt;array&gt;
      &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
  &lt;/array&gt;
  </code></pre>
  <p>Then I defined an AppDelegate. Still no success, the UI keeps rotating on the iPad.</p>
  <pre><code>class AppDelegate: NSObject, UIApplicationDelegate {
      func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -&gt; UIInterfaceOrientationMask {
          return UIInterfaceOrientationMask.portrait
      }
  }
  
  @main
  struct orientationApp: App {
      @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
      var body: some Scene {
          WindowGroup {
              ContentView()
          }
      }
  }
  </code></pre>
  <p>What is interesting:</p>
  <ul>
  <li>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.</li>
  <li>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).</li>
  </ul>
  <p>I'm using Xcode 12.5 and Swift 5.</p>