如何解决为什么SwiftUI呈现的视图显示不正确?
我已经从Xcode 12 beta 6(12A8189n)应用模板创建了一个SwiftUI“多平台”(iOS和macOS)应用。
我的问题是我的一个视图AnotherView
显示不正确。这是显示问题的GIF。请注意,AnotherView
显示的导航堆栈已被推送到不存在的视图。轻按后退按钮可显示预期的屏幕,但是仅部分填充了预期的区域。
这是代码:
TestNavigationApp.swift
import SwiftUI
@main
struct TestNavigationApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@State private var presentingFirstView = false
var body: some View {
Button(action: { self.presentingFirstView = true }) {
Text("Present First View")
}
.sheet(isPresented: $presentingFirstView) {
FirstView(isPresented: $presentingFirstView)
}
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
@Binding var isPresented: Bool
var body: some View {
NavigationView {
EmbeddedView()
.navigationBarTitle("First View",displayMode: .large)
}
}
}
EmbeddedView.swift
import SwiftUI
struct EmbeddedView: View {
@State private var presentingAnotherView = false
var body: some View {
VStack {
Text("Embedded View")
Button(action: { self.presentingAnotherView = true }) {
Text("Present Another View")
}
}
.sheet(isPresented: $presentingAnotherView) {
AnotherView(isPresented: $presentingAnotherView)
}
}
}
AnotherView.swift
import SwiftUI
struct AnotherView: View {
@Binding var isPresented: Bool
var body: some View {
NavigationView {
Text("Another View")
.navigationBarTitle("Another View",displayMode: .large)
}
}
}
无论如何,不是很确定这里发生了什么。任何建议表示赞赏。
解决方法
尝试明确使用导航视图样式
var body: some View {
NavigationView {
Text("Another View")
.navigationBarTitle("Another View",displayMode: .large)
}.navigationViewStyle(StackNavigationViewStyle())
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。