Я хочу, чтобы представление, содержащее 2 прямоугольника, плавающих в нижней части экрана, независимо от ориентации, книжной или альбомной.
Код - это тест, когда происходит ориентацияDidChangeNotification, я нашел UIDevice.current.orientation.isPortrait и UIScreen.main.bounds.height часто имеют неправильное значение, почему?
В любом случае, тестовый код просто сбрасывается смещение = 0 в onRotated (). но это не работает; в противном случае onTapGesture работает нормально.
В1: Это неверный путь для SwiftUI? SceneDelegate.orientationDidChangeNotification -> contentView.onRotated ()?
Q2: почему UIDevice.current.orientation.isPortrait и UIScreen.main.bounds.height часто имеют неправильное значение?
Q3: как чтобы вид плавал в нижней части экрана как в книжной, так и в альбомной ориентации?
let height: CGFloat = 100
struct TestView: View {
@State var offset = (UIScreen.main.bounds.height - height) / 2
var body: some View {
ZStack {
Text("+")
VStack(spacing: 0) {
Rectangle().fill(Color.blue)
Rectangle().fill(Color.red)
}
.frame(width: 100, height: height)
.offset(y: offset)
.onTapGesture {
self.offset = 0
}
}
}
func onRotated() {
// let isPortrait = UIDevice.current.orientation.isPortrait
offset = 0//(UIScreen.main.bounds.height - height) / 2
// print("\(isPortrait), screen height = \(UIScreen.main.bounds.height)")
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = TestView()
if let windowScene = scene as? UIWindowScene {
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: nil) { notification in
contentView.onRotated()
}
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
...
}