gpt4 book ai didi

ios - 带有导航链接的 SwiftUI 消失后退按钮

转载 作者:行者123 更新时间:2023-12-04 01:03:31 26 4
gpt4 key购买 nike

我有 3 个观点。其中一个有 NavigationView,第二个有 NavigationLink,最后一个只有工具栏的 child 。
所以当我在最后一个 View backButton 优雅中添加工具栏时,我的问题消失了。我该如何解决这个问题?
Screen recording of my problem

import SwiftUI

struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello, world!")
.padding()
NavigationLink(destination: ListView()) {

Image(systemName: "trash")
.font(.largeTitle)
.foregroundColor(.red)
}
}.navigationBarHidden(true)
.navigationTitle("Image")
}
}
}
import SwiftUI

struct ListView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

var body: some View {
VStack {
List {
NavigationLink(destination: DetailView()) {
Text("Detail")
}
}

}.navigationBarTitle(Text("Data"), displayMode: .large)
.toolbar {
Button("Save") {
presentationMode.wrappedValue.dismiss()
}
}
}
}
import SwiftUI

struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {

VStack {
Text("DetailView")
.padding()
}.navigationBarTitle(Text("Data"), displayMode: .large)
.toolbar {
Button("Save") {
presentationMode.wrappedValue.dismiss()
}
}
}
}

最佳答案

在控制台中,您会注意到此消息:

2021-04-27 12:37:36.862733-0700 MyApp[12739:255441] [Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.

NavigationView 的默认样式通常是 DefaultNavigationViewStyle , which is really just DoubleColumnNavigationViewStyle .使用 StackNavigationViewStyle相反,它按预期工作。
编辑:你说得对 StackNavigationViewStyle将打破 iPad 拆分 View 。但幸运的是, DoubleColumnNavigationViewStyle在 iPad 上工作正常,并且不会隐藏后退按钮。然后我们可以使用不同的 NavigationStyle取决于设备,如 this answer 中所示.
struct ResponsiveNavigationStyle: ViewModifier {
@Environment(\.horizontalSizeClass) var horizontalSizeClass

@ViewBuilder
func body(content: Content) -> some View {
if horizontalSizeClass == .compact { /// iPhone
content.navigationViewStyle(StackNavigationViewStyle())
} else { /// iPad or larger iPhone in landscape
content.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
}

struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello, world!")
.padding()
NavigationLink(destination: ListView()) {

Image(systemName: "trash")
.font(.largeTitle)
.foregroundColor(.red)
}
}
.navigationBarHidden(true)
.navigationTitle("Image")
}
.modifier(ResponsiveNavigationStyle()) /// here!
}
}
结果:


iPad
苹果手机


iPad split view working and back button not hidden
Back button also not hidden

关于ios - 带有导航链接的 SwiftUI 消失后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67289448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com