SwiftUI - Determining Current Device and Orientation(SwiftUI - 确定当前设备和方向)
问题描述
我正在尝试检测设备何时在 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.
这篇关于SwiftUI - 确定当前设备和方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SwiftUI - 确定当前设备和方向


- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01