tl; dr swiftui datapageview встроен в приложение uikit с использованием uihostingcontroller . Как DatapageView представить chartview полноэкранный режим ландшафта? Это сделано шаг за шагом. В приложении по -прежнему используется макет на основе uitabbar , и в настоящее время я работаю над заменой одной вкладки ViewController на uihostingController Swiftui View. /> DataPageViewControllers реагирует на изменение ориентации на ландшафт, представляя chartsViewController , которые показывают данные в разных диаграммах. Теперь я хотел бы имитировать это поведение в swiftui .
Теперь uihostingcontroller (rootview: datapageview ()) заменяет DataPageViewControllers и я пытался добавить .fullScreencover , чтобы показать карту..fullScreenCover представлен при вращении datapageview () для ландшафта), содержание обложки всегда появляется в портрете. UiviewControllerRepresentable Какой засильно не работает ландшафт:
// DataView - portrait
VStack {
...
}
.onChange(of: orientationObserver.isLandscape) { isLandscape in
if isLandscape {
viewModel.showCharts()
}
}
ChartsView(isPresented: $viewModel.isShowingCharts)
// DataView - landscape
struct ChartsView: View {
@StateObject var orientationObserver = OrientationObserver()
@Binding var isPresented: Bool
var body: some View {
ZStack {}
.fullScreenCover(isPresented: $isPresented) {
ForceLandscapeView {
charts()
}
}
.onChange(of: orientationObserver.isLandscape) { isLandscape in
if !isLandscape {
print("Dismiss in EntriesChartsView")
isPresented = false
}
}
}
@ViewBuilder
func charts() -> some View {
Text(orientationObserver.isLandscape ? "Landscape" : "Portrait")
}
}
class OrientationObserver: ObservableObject {
@Published var isLandscape = false
private var cancellable: AnyCancellable?
init() {
cancellable = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)
.sink { _ in
let orientation = UIDevice.current.orientation
if orientation.isValidInterfaceOrientation {
self.isLandscape = orientation.isLandscape
}
}
}
}
struct ForceLandscapeView: UIViewControllerRepresentable {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
func makeUIViewController(context: Context) -> UIViewController {
LandscapeHostingController(rootView: content)
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
class LandscapeHostingController: UIHostingController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeRight
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... tui-screen