SwiftUI - 确定当前设备和方向

人气:662 发布:2022-10-16 标签: ios swiftui uikit

问题描述

我正在尝试检测设备何时在 iPad 上和纵向模式.

I am trying to detect when the device is on iPad and in Portrait.

目前我在 UIKit 中使用 UIDevice API 并使用环境对象来观察变化.我使用此处找到的解决方案 - 确定当前设备和方向.

Currently I use the UIDevice API in UIKit and use an environment object to watch changes. I use the solution found here - Determining Current Device and Orientation.

但是,orientationInfo.orientation 最初总是等于 .portrait,直到旋转为纵向,然后再返回横向.

However the orientationInfo.orientation is initially always equal to .portrait until rotated into portrait and then back to landscape.

所以当执行以下操作来显示FABView

So when doing the following to display the FABView

struct HomeView: View {

@EnvironmentObject var orientationInfo: OrientationInfo
let isPhone = UIDevice.current.userInterfaceIdiom == .phone

    var body: some View {
      ZStack(alignment: .bottom) {
          #if os(iOS)
          if isPhone == false && orientationInfo.orientation == .portrait {
            FABView()
          }
          #endif
      }
    }
}

当 iPad 最初处于横向时会加载视图,但在更改为纵向并返回横向时会被删除.为什么会发生这种情况?如何确保在首次加载时未加载视图?

The view is loaded when the iPad is initially in landscape, but when changing to portrait and back to landscape is then removed. Why is this happening and how can I make sure the view isn't loaded on first load ?

完整代码

struct HomeTab: View {
    
    var body: some View {
        NavigationView {
            HomeView()
                .environmentObject(OrientationInfo())
        }
    }
}

struct HomeView: View {

@EnvironmentObject var orientationInfo: OrientationInfo
let isPhone = UIDevice.current.userInterfaceIdiom == .phone

    var body: some View {
      ZStack(alignment: .bottom) {
          #if os(iOS)
          if isPhone == false && orientationInfo.orientation == .portrait {
            FABView()
          }
          #endif
      }
    }
}

final class OrientationInfo: ObservableObject {
    enum Orientation {
        case portrait
        case landscape
    }
    
    @Published var orientation: Orientation
    
    private var _observer: NSObjectProtocol?
    
    init() {
        // fairly arbitrary starting value for 'flat' orientations
        if UIDevice.current.orientation.isLandscape {
            self.orientation = .landscape
        }
        else {
            self.orientation = .portrait
        }
        
        // unowned self because we unregister before self becomes invalid
        _observer = NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: nil) { [unowned self] note in
            guard let device = note.object as? UIDevice else {
                return
            }
            if device.orientation.isPortrait {
                self.orientation = .portrait
            }
            else if device.orientation.isLandscape {
                self.orientation = .landscape
            }
        }
    }
    
    deinit {
        if let observer = _observer {
            NotificationCenter.default.removeObserver(observer)
        }
    }
}

推荐答案

您可以使用 UIDevice.orientationDidChangeNotification 来检测方向变化,但您不应该在应用启动时依赖它.

You can use UIDevice.orientationDidChangeNotification for detecting orientation changes but you shouldn't rely on it when the app starts.

UIDevice.current.orientation.isValidInterfaceOrientation 一开始会是假的,因此两者都是

UIDevice.current.orientation.isValidInterfaceOrientation will be false at the beginning and therefore both

UIDevice.current.orientation.isLandscape

UIDevice.current.orientation.isPortrait

将返回 false.

相反,您可以使用第一个窗口场景中的 interfaceOrientation:

Instead you can use interfaceOrientation from the first window scene:

struct ContentView: View {
    @State private var isPortrait = false
    
    var body: some View {
        Text("isPortrait: (String(isPortrait))")
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                guard let scene = UIApplication.shared.windows.first?.windowScene else { return }
                self.isPortrait = scene.interfaceOrientation.isPortrait
            }
    }
}

还要注意设备方向不等于界面方向.当设备在纵向模式下倒置时,设备方向是portrait,但界面方向也可以是landscape.

Also note that device orientation is not equal to interface orientation. When the device is upside down in portrait mode, device orientation is portrait but interface orientation can be landscape as well.

我认为在你的情况下依赖界面方向会更好.

I think it's better to rely on the interface orientation in your case.

488