swiftui - 为什么 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()
}
}
}
内容视图.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)
}
}
}
另一个视图.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())
}
推荐阅读
- c# - Unity3D:如何修复“TerrainData.SetHeights 错误”
- cookies - 如何保存 cookie 并将其加载到另一个 puppeteer 会话中?
- ruby-on-rails - 如何在这个可过滤的分页 Rails API 中优化这个活动记录查询?
- excel - 保存数据时如何弹出“数据保存成功”消息?
- r - 由于记录数量而转换为事务性能问题
- python - 如何在基于海龟的 Pong 游戏上实现 TcP 连接,以创建 2 人游戏?
- scala - 为什么Failure case 从来没有达到?
- reactjs - 函数外的 React-Apollo 突变数据?
- python - 那么 float8、float16、float32、float64 和 float128 究竟可以包含多少位?
- dart - Dart 2.2 之后如何设置文字