为什么 SwiftUI 呈现的视图显示不正确?
Why is SwiftUI presented view displayed incorrectly?
我从 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())
}
我从 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())
}