【问题标题】:swiftUI bottomBar toolbar disappears when going back返回时swiftUI bottomBar工具栏消失
【发布时间】:2020-12-03 13:32:49
【问题描述】:

我有这些 swiftUI 视图并尝试使用 toolbar (bottomBar)。当您启动应用程序时,它看起来很好,但是在使用他的 navigationLink 转到 View2 然后返回主视图后,工具栏就会消失。当 NavigationLink 在列表中时会发生这种情况。如果您不使用列表(将导航链接放在 VStack 或类似内容中),它会按预期工作,并且当您返回初始视图时工具栏会重新出现。有没有办法解决这个问题?

import SwiftUI

struct View2: View {
    var body: some View {
        VStack{
            Text("View2")
        }
        
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView{
            List{
                NavigationLink(destination: View2()) {
                    Text("go to View2")
                }
                
            }
            .toolbar(content: {
                ToolbarItem(placement: .bottomBar, content: {
                    Text("toolbar item 1")
                })
            })
        }
        .navigationViewStyle(StackNavigationViewStyle())
            
    }
}

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

【问题讨论】:

标签: ios swiftui


【解决方案1】:

这是已知的错误。这是可能的解决方法 - View2 上的强制刷新消失了(使用 Xcode 12.1 / iOS 14.1 测试)

struct ContentView: View {
    @State private var refresh = UUID()

    var body: some View {
        NavigationView{
            List{
                NavigationLink(destination:
                        View2().onDisappear { refresh = UUID() }) { // << here !!
                    Text("go to View2")
                }
            }
            .toolbar(content: {
                 ToolbarItem(placement: .bottomBar, content: {
                      Text("toolbar item 1")
                 })
            }).id(refresh)                     // << here !!

        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

【讨论】:

  • 不错的解决方法。它也适用于 xCode 12.2
  • 对我不起作用。 XCode 12.4 (12D4e)。
  • 在 Xcode 12.4 中为我工作 - 几乎忘记包含刷新 ID 的 onDisappear 方法。谢谢!
  • 有什么解决方案或建议吗? @asperi 的解决方案不适用于我的情况
【解决方案2】:

就我而言,我必须确保工具栏在 View1 中可见。为了让这个工作正常进行,我使用了一个状态变量来检测 View1 何时被用户查看。

 struct View2: View {
    var body: some View {
        Text("View2")
    }
 }

struct View1: View {
    @State private var refresh = UUID()
    @State var isShown = true
    
    var body: some View {
            NavigationView {
                List {
                    NavigationLink(destination:
                        View2()
                            .onDisappear(perform: {
                                    if (isShown) {
                                        refresh = UUID()
                                    }
                            })
                            .onAppear(perform: { isShown = false })
                    ){
                       Text("Row1")
                    }
                }
                .onAppear(perform: {
                    isShown = true
                })
                .toolbar(content: {
                    ToolbarItem(placement: .bottomBar) {
                    Button(action: {
                        // Action
                    }) {
                        HStack(spacing: 10) {
                        Image(systemName: "plus.circle")
                        Text("Add")
                    }
                    }}
                })
                .id(refresh)
            }
    }
}

【讨论】:

    【解决方案3】:

    这种方式比较简单,可以帮助一些人。

    .toolbar {
          ToolbarItem(id: UUID().uuidString, placement: .bottomBar, showsByDefault: true) {
                AssetToolbarView(selectedCount: 0)
          }        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-13
      • 2022-10-15
      • 2012-04-18
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多