3.3 C
New York
Sunday, December 8, 2024

ios – Lock orientation of a view to solely panorama


I’m making an attempt to current a view in panorama orientation from a portrait view utilizing the under code

   Button {
      isLandscapeViewPresented.toggle()
   } label: {
      Textual content("Click on for panorama view")
   }.fullScreenCover(isPresented: $isLandscapeViewPresented) {
     LandscapeOnlyView {
        LandscapeView()
     }
   }

And in LandscapeOnlyView wrapper I’ve as under

struct LandscapeOnlyView: UIViewControllerRepresentable {
    
    let content material: Content material
    
    init(@ViewBuilder content material: () -> Content material) {
        self.content material = content material()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
    
    
    func makeUIViewController(context: Context) -> some UIViewController {
        return LandscapeHostingController(rootView: content material)
    }
}

For LandscapeHostingController which holds the rootview as under:

class LandscapeHostingController: UIHostingController {
        
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .panorama
    }
        
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        AppDelegate.orientationLock = .panorama
        let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        scene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight), errorHandler: { error in
            print(error)
        })
    }
        
    override func viewWillDisappear(_ animated: Bool) {
        tremendous.viewWillDisappear(animated)
        
        AppDelegate.orientationLock = .portrait
        let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        scene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait), errorHandler: { error in
            print(error)
        })
    }
}

I’m utilizing AppDelegate, as you may see the place I replace the orientationLock, with supportedInterfaceOrientations override returning the up to date orientation at any time when I want to alter orientations.

I get the under output.

Sample

I want the LandscapeView to be in panorama even earlier than presenting in contrast to what’s present above the place the view switches to panorama because the orientation replace is finished in viewWillAppear of the LandscapeHostingController wrapper for a swift ui view.

I’ve tried some ways. Is there one thing unsuitable in my strategy which must be modified.

Any concepts on how that it’s achieved?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles