UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.