UIInterfaceOrientation not working on iPad iOS 16

245 Views Asked by At

Please help. I'm trying to build custom views for portrait and landscape orientation with different offsets of ui elements. Will not use UIDevice.current.orientation as there is no way to disable .isFlat and not change the orientation when laying flat.

For UIInterfaceOrientation I'm trying to build a ZStack {} with custom settings in Landscape and Portrait, but I get error:

"Cannot find 'windowInterfaceOrientation' in scope"

please help :) Just in case, I don't have SceneDelegate as I'm only using SwiftUI.

Here is my code:

import SwiftUI
import UIKit


class ViewController: UIViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    
    coordinator.animate(alongsideTransition: { (context) in
        guard let windowInterfaceOrientation = self.windowInterfaceOrientation else { return }
        
        if windowInterfaceOrientation.isLandscape {
                // activate landscape changes
            } else {
                // activate portrait changes
            }
        })
    }
    
    private var windowInterfaceOrientation: UIInterfaceOrientation? {
        return UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
    }
}


struct v_2: View {
    
    
    
    var body: some View {
        
        
        ZStack {
            if windowInterfaceOrientation.isLandscape {
                // activate landscape changes
                Text ("Landscape - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis elementum lorem. Nullam fermentum viverra ipsum a finibus. Vestibulum venenatis risus vel leo sagittis, id aliquet tortor elementum. Aenean elementum orci ac sapien dictum laoreet. Sed placerat, magna sit amet eleifend auctor, odio ligula dapibus lacus, quis interdum ligula velit at est. Curabitur molestie dui sodales faucibus cursus. Duis posuere ex diam, tempor tincidunt nunc venenatis vitae. Integer vulputate odio vitae enim tincidunt, eget vulputate arcu pulvinar. Integer in magna erat.")
                    .frame(width: 500, height: 300)
                    .background(
                Rectangle()
                    .fill(Color.red))
            } else {
                Text ("Portrait - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin quis elementum lorem. Nullam fermentum viverra ipsum a finibus. Vestibulum venenatis risus vel leo sagittis, id aliquet tortor elementum. Aenean elementum orci  Integer in magna erat.")
                    .frame(width: 500, height: 300)
                    .background(
                Rectangle()
                    .fill(Color.blue))
            }
        }
    }
}
  

struct v_2_Previews: PreviewProvider {
    static var previews: some View {
        v_2()
    }
}
1

There are 1 best solutions below

0
CodingTh On

I've spent a week trying to find a solution and got interfaceOrientation working on an iPad. There is warning 'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead. - will deal with it later. Hope might help someone:

in struct use:

@State private var orientation = UIApplication.shared.statusBarOrientation
    private let orientationChanged = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)

add to the last object like VStack {}

.onAppear {
                if let scene = UIApplication.shared.connectedScenes.first,
                   let sceneDelegate = scene as? UIWindowScene,
                   sceneDelegate.interfaceOrientation.isPortrait {orientation = .portrait
                } else {orientation = .landscapeLeft}}
            .onReceive(orientationChanged) { _ in orientation = UIApplication.shared.statusBarOrientation}

So when app launches .onAppear we use interfaceOrientation and when we rotate the screen .onReceive we also use interfaceOrientation which is represented (need to update as was deprecated but works!) through UIApplication.shared.statusBarOrientation