我正在尝试实现从导航视图链接的详细视图。在此详细视图中,顶部有一个带有后退按钮的默认导航栏。 但当我向上滚动时,该栏仅显示一些颜色。我不知道为什么。
最初,导航栏在滚动或不滚动时都没有背景。所以我创建了一个 init() 方法来设置样式。
init(fruit: Fruit) {
self.fruit = fruit
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}
Run Code Online (Sandbox Code Playgroud)
主体视图是外部的导航视图和内部的滚动视图。** 对于任何人来说,为什么我将导航栏设置为隐藏在 VStack 中,是因为如果我不隐藏它,图像上方将会有一些巨大的空间。(我不知道为什么)

** 更新的代码 ** 我更新了使用不透明背景的代码。但似乎这些配置都不可见。
init(fruit: Fruit) {
self.fruit = fruit
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
UINavigationBar.appearance().standardAppearance = navBarAppearance
}
var body: some View {
NavigationView {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 20) {
// HEADER
FruitHeaderView(fruit: …Run Code Online (Sandbox Code Playgroud) 我正在使用 @State 变量来存储一些导航链接的标签。但在 ios 15 中,我注意到每当我进入后台时,状态变量都会被重置,这导致当我返回应用程序时,视图会无缘无故地弹出。
@State private var userNavigateSelection: String? = nil
NavigationLink(destination: TestView(), tag: TestTag, selection: $userNavigateSelection) { EmptyView() }
Run Code Online (Sandbox Code Playgroud)
每次进入后台时,我都使用 onChange 方法来监视“userNavigateSelection”的变化。“userNavigationSelection”将更改为 nil。我不知道为什么。