Код: Выделить всё
Button {
isLandscapeViewPresented.toggle()
} label: {
Text("Click for landscape view")
}.fullScreenCover(isPresented: $isLandscapeViewPresented) {
LandscapeOnlyView {
LandscapeView()
}
}
Код: Выделить всё
struct LandscapeOnlyView: UIViewControllerRepresentable {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
func makeUIViewController(context: Context) -> some UIViewController {
return LandscapeHostingController(rootView: content)
}
}
Код: Выделить всё
class LandscapeHostingController: UIHostingController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppDelegate.orientationLock = .landscape
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
scene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight), errorHandler: { error in
print(error)
})
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppDelegate.orientationLock = .portrait
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
scene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait), errorHandler: { error in
print(error)
})
}
}
Я получаю следующий результат.

Мне нужно, чтобы LandscapeView находился в альбомной ориентации еще до представления, в отличие от того, что показано выше, где представление переключается на альбомное, поскольку обновление ориентации выполняется в viewWillAppear оболочки LandscapeHostingController для быстрого просмотра пользовательского интерфейса.Я пробовал много способов. Есть ли что-то неправильное в моем подходе, и его следует изменить?
Есть идеи, как этого добиться?
Подробнее здесь: https://stackoverflow.com/questions/792 ... -landscape