跟随 NavigationLink 并返回后 SwiftUI .toolbar 消失

2023-12-27

我已将 .toolbar 添加到 NavigationView 的顶层,最终将用于选择列表中的项目,而无需使用滑动手势(向上按钮、向下按钮等)。我还有一个 .navigationBar 正在进行,用于访问帐户和设置的其他视图。

在大多数情况下,它看起来非常好,但是当我在 NavigationView 中关注 NavigationLink(在 .navigationBarItems 中)时,然后使用内置的后退导航,我的 .toolbar 从顶层消失。

我是否将 .toolbar 放在了错误的位置?感觉像是 .navigationViewStyle(StackNavigationViewStyle()) 的问题,因为当我注释掉它时,工具栏不会在导航时消失......但我不喜欢默认行为在横向中的工作方式,所以我依赖它。


import SwiftUI

struct ContentView: View {

    var body: some View {

                NavigationView {

                    List {
                        Group {
                            Section(header: Text("List Items").foregroundColor(.gray).font(.footnote)) {
                                Text("List Item One")
                                Text("List Item Two")
                                Text("List Item Three")
                            }
                        }
                   }.navigationTitle("Top Level List").navigationBarTitleDisplayMode(.inline)
                       .ignoresSafeArea(.all)

    // MARK: NAVBAR

                        .navigationBarItems(
                            leading:
                            NavigationLink(destination: UserView()) {
                                Image(systemName: "person.crop.circle").font(.title2)
                            },
                            trailing:
                                NavigationLink(destination: SettingsView()) {
                                    Image(systemName: "gear").font(.title2)
                                })

     //MARK: - CONTENT NAV

                        .toolbar {

                            ToolbarItemGroup(placement: .bottomBar) {

                                Button(action: {}, label: {Label("Mute", systemImage: "speaker.slash.fill")})
                                Spacer()
                                Button(action: {}, label: {Label("Repeat", systemImage: "arrow.clockwise")})
                                Spacer()
                                Button(action: {}, label: {Label("Previous", systemImage: "arrow.up")})
                                Spacer()
                                Button(action: {}, label: {Label("Next", systemImage: "arrow.down")})
                                Spacer()
                                Button(action: {}, label: {Label("Select", systemImage: "arrow.right")})

                            }
                        }
                }.navigationViewStyle(StackNavigationViewStyle())
        }
}

struct UserView: View {

    @State private var username: String = ""
    @State private var password: String = ""

    var body: some View {

                    Form {
                        TextField("Username", text: $username)
                        SecureField("Password", text: $password)
                    }
                    .navigationBarTitle("Account").font(.subheadline)

    }
}

struct SettingsView: View {
    
    @State private var setting1: String = ""
    @State private var setting2: String = ""

    var body: some View {

        Form {
            TextField("Setting One", text: $setting1)
            SecureField("Setting Two", text: $setting2)
        }
        .navigationBarTitle("Settings").font(.subheadline)

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

你是对的,它在错误的地方。如果您需要始终显示工具栏,则应如下所示:

struct ContentView: View {
    
    var body: some View {
        
        NavigationView {
            
            List {
                Group {
                    Section(header: Text("List Items").foregroundColor(.gray).font(.footnote)) {
                        Text("List Item One")
                        Text("List Item Two")
                        Text("List Item Three")
                    }
                }
            }.navigationTitle("Top Level List").navigationBarTitleDisplayMode(.inline)
                .ignoresSafeArea(.all)
            
            // MARK: NAVBAR
            
                .navigationBarItems(
                    leading:
                        NavigationLink(destination: UserView()) {
                            Image(systemName: "person.crop.circle").font(.title2)
                        },
                    trailing:
                        NavigationLink(destination: SettingsView()) {
                            Image(systemName: "gear").font(.title2)
                        })
            
            //MARK: - CONTENT NAV
            
        }.navigationViewStyle(StackNavigationViewStyle())
            .toolbar {
                
                ToolbarItemGroup(placement: .bottomBar) {
                    
                    Button(action: {}, label: {Label("Mute", systemImage: "speaker.slash.fill")})
                    Spacer()
                    Button(action: {}, label: {Label("Repeat", systemImage: "arrow.clockwise")})
                    Spacer()
                    Button(action: {}, label: {Label("Previous", systemImage: "arrow.up")})
                    Spacer()
                    Button(action: {}, label: {Label("Next", systemImage: "arrow.down")})
                    Spacer()
                    Button(action: {}, label: {Label("Select", systemImage: "arrow.right")})
                    
                }
            }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

跟随 NavigationLink 并返回后 SwiftUI .toolbar 消失 的相关文章

随机推荐

  • Java 反射和重构中的痛苦

    Java Reflection 提供了一种在运行时自省对象的机制 毫不犹豫 这是一个很棒的功能 但它打破了所有重构约定 没有简单的方法 除了File Search 甚至在现代 IDE 中也能知道引用了哪个属性以及在哪里引用 这使得重构变得更
  • apache,允许文件名带有“?”

    我想制作网站的静态副本 保留现有的 URL 问题是 URL 看起来像 http mysite index php id XXX http mysite index php id XXX Apache 不想找到文件 index php id
  • 如何知道Android是否连接到WiFi或以太网? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 How to know whether I m connected to WiFi or ethernet in Android In
  • 使用一种编译器创建的静态 C 库是否与另一种编译器兼容?

    就我而言 我有一个使用代码源 gcc 构建的库 目标是arm cortex m4 然后我尝试将该库链接到使用 IAR 编译器编译的项目中 是否可以做到这一点 或者是否必须使用新工具重建库 哪些因素影响这个 静态库是几个目标文件的捆绑 这些目
  • Seaborn 散点图将气泡大小缩放为更大的点

    我正在关注这个example https seaborn pydata org generated seaborn scatterplot html我想创建更大的气泡 但无论我将尺寸列乘以多大 它们仍然很小 是否需要调整某种比例因子 我在文
  • 为什么这个 IFrame 没有占据完整的页面高度? [复制]

    这个问题在这里已经有答案了 我正在使用 Iframe 来显示 google com 或说任何网站 我使用了 height 100 但即便如此 我的 Iframe 大小也只是页面的一半 请让我知道为什么会发生这种情况 链接在这里 http j
  • Spark:测试 RDD 是否为空的有效方法

    没有一个isEmptyRDD 上的方法 那么测试 RDD 是否为空的最有效方法是什么 RDD isEmpty https issues apache org jira browse SPARK 5270将成为 Spark 1 3 0 的一部
  • 与PHP在同一页面显示结果

    我基本上是从 PHP 开始学习的 我想做的事情很简单 我想在同一页面显示计算结果 这是项目的结构 https i stack imgur com XGPVr png Code 索引 php header php
  • WAMP、Laravel 5:路由不起作用 - NotFoundHttpException

    我在本地计算机上使用 Laravel 5 和 WAMP 没有虚拟化 使用 Composer 创建一个名为 LaravelProject 的新项目后 我似乎根本无法让我的路线正常工作 我已经做了什么 Edited httpd conf to
  • 查找:显示模板速度

    我在新的 MVC4 站点上安装了 Mini Profiler 并注意到某些功能的等待时间很长Find DisplayTemplates包括字符串和日期时间 下面是一个例子 在另一个问题中 Sam Saffron 谈到了查找步骤 在随后的运行
  • 理解c++20中的convertible_to概念

    我对 C 20 概念仍然很陌生 我想知道为什么这不起作用 我想创建一个将数字连接为字符串的函数模板 所以我想尝试一些概念 我用了std convertible to检查输入的数据类型 即int在这种情况下 可以转换为std string 但
  • UICollectionView 标题宽度

    在我的 UICollectionViewFlowLayout 子类中 我有这个 self headerReferenceSize CGSizeMake 280 44 但是 标题的显示宽度为 320 这是集合视图的宽度 根据文档 这是正确的
  • 处理 Express 表单中的输入数组?

    假设我必须在同一页面上编辑一批相同类型的对象 jade form action method POST for each message id in messages ids input type text name message id
  • 如何删除Android searchview左侧的空间(不属于actionbar的一部分)?

    我在 android 应用程序中使用 searchview searchview 不是操作栏的一部分 我想删除搜索图标 搜索视图左侧的空格 我搜索了很多并收到了适用于作为操作栏一部分的搜索视图的答案 我尝试使用机器人 布局重力 机器人 重力
  • 为什么 const char* const & = "hello" 可以编译?

    我正在阅读一本书中的代码片段并发现 const char const a hello can compile const char a hello cannot 我所知道的是 在初始化引用时 数组到指针的转换不会发生 const char
  • graphstudio 中可用的引脚在代码中不存在

    我正在使用网络摄像头的源过滤器 当我在 graphstudio 中使用过滤器时 它有两个输出引脚 然而 在代码中 对 IEnumPins gt next 的调用始终返回 S FALSE 我还寻找了另一个可以创建引脚的接口 但没有找到这样的东
  • PHP S3上传进度

    这种情况已经发生过很多次了 但我仍然有点困惑 很多答案只关注谈论上传进度条 而不是从 S3 上传获取实际的上传进度 我已经阅读了很多问题并找到了很多软件 但我仍然没有更深入地理解 S3 上传的基本问题 有没有一种方法可以上传到 S3 同时了
  • “TypeError:item.getAttachmentsAsync 不是函数”Outlook 加载项 office-js 与 Vue

    我一直在关注此链接中的教程 https learn microsoft com en us javascript api outlook office messageread view outlook js preview getAttac
  • 如何删除jquery添加的样式属性

    我正在使用具有一些自定义要求的 devExpress 表 更新 休息了一天 然后回去并使用 React Styling 正确完成了它 感谢您的建议 在屏幕截图中 我禁用了某些单元格 但是 用户希望除所选行之外的所有单元格看起来均已禁用 使用
  • 跟随 NavigationLink 并返回后 SwiftUI .toolbar 消失

    我已将 toolbar 添加到 NavigationView 的顶层 最终将用于选择列表中的项目 而无需使用滑动手势 向上按钮 向下按钮等 我还有一个 navigationBar 正在进行 用于访问帐户和设置的其他视图 在大多数情况下 它看