WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Really struggling with orientation and fixed orientation

Forums > SwiftUI

I am doing an update of an app I have in the app store.

I'm adding SwiftUI to some new screens in the app, the first being the 'What's New' screen.

Because my app supports iPhone SE 1st Gen and everything up to the 12.9" iPad Pro I really, really don't want the 'What's New' showing up in landscape if they launch the app in a landscape orientation because there isn't enough screen space to show everything I want to do.

So I had the idea of making the what's new viewcontroller locked to portrait and the swiftUI inside of it locked to portrait.

This is working perfectly if I launch the app on a device already in portrait, but when I launch it in with the device already in landscape, the swift UI view is mostly rendered off screen in a landscape frame but not the same way it would have been if I supported landscape as normal i.e. it's not centred in the view.

Can anyone please help?

This is the code in the app delegate

  static var orientationLock = UIInterfaceOrientationMask.all

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

this is the code in the UIViewController

  override func viewDidLoad()
  {
    super.viewDidLoad()

    addChild(contentView)
    contentView.view.frame = view.bounds

    view.addSubview(contentView.view)
  }

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask
  {
      if PBFMenuSettingsModel.shared.getDoesWhatsNewNeedToBeShown() == YES
      {
          if UIDevice.current.userInterfaceIdiom == .phone
          {
              return .portrait
          }
      }

      return .all
  }

  override func viewWillAppear(_ animated: Bool)
  {
    let value = UIInterfaceOrientation.portrait.rawValue

    UIDevice.current.setValue(value, forKey: "orientation")
   }

and this is the code in the SwiftUI View

      Group
      {
        VStack
        {
          Group
          {
            Text("What's New?")
              .padding(.top, 5)
          }

          Group
          {
            TabView(selection: $selectedPage)
            {
              Page1().tag(0)
              Page2().tag(1)
              Page3().tag(2)
              Page4().tag(3)
            }
            .tabViewStyle(.page)
            .indexViewStyle(.page(backgroundDisplayMode: .always))
            .padding(.top, -10)
          }

          Group
          {
            Button(buttonLabel)
            {
              withAnimation
              {
                if self.selectedPage == numberOfPages
                {
                  return
                }
                selectedPage += 1
              }
            }
            .animation(.default, value: buttonLabel)
            .buttonStyle(.borderedProminent)
            .foregroundColor(.white)
            .padding([.top, .bottom], pageViewPadding)
          }
        }
        .onAppear()
        {
          AppDelegate.orientationLock = .portrait
        }
        .onDisappear()
        {
          AppDelegate.orientationLock = .all
        }
      }

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.