SwiftUI-确定当前设备和方向 [英] SwiftUI - Determining Current Device and Orientation
问题描述
我正在尝试检测设备何时在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
开头将为false,因此两者均为
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
}
}
}
还请注意,设备方向不等于接口方向.当设备在纵向模式下倒置时,设备方向为人像,但界面方向也可以为 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.
这篇关于SwiftUI-确定当前设备和方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!