NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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
        }
      }

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.