繁体   English   中英

SwiftUI - 点击 NavigationLink 后底栏为空

[英]SwiftUI - Empty bottom bar after tapping NavigationLink

我有一个带有toolbarNavigationView ,其中包含一个带有ToolBarItem位置的.bottomBar和一个搜索字段。 这个NavigationView包含一个ScrollView ,其内容超出了屏幕的垂直尺寸,这意味着底部栏有背景,如下所示:

根视图截图

当用户点击“Root View”文本元素时,他们导航到一个新视图,在本例中,只是另一个显示“Detail View”的文本。 然而,问题是底部工具栏的背景保留在屏幕中,而不是像预期的那样消失。 请参见下面的屏幕截图:

在此处输入图像描述

如果我删除搜索栏或缩小ScrollView的高度以适合设备的垂直尺寸,则不会看到此行为。 我试着用谷歌搜索这个问题,看看它是否是一个有解决方法的已知错误,但也许我没有搜索正确的关键字。 我该如何解决这个问题?

请查看最低限度以复制以下问题:

struct BugView: View {
    @State var searchPattern: String = ""
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    NavigationLink(destination: Text("Detail View")) {
                        Text("Root View").foregroundColor(Color.blue)
                    }
                    Spacer()
                    Text("Root View Bottom").foregroundColor(Color.blue)
                }.frame(maxWidth: .infinity, minHeight: 1000)
            }
                .searchable(text: self.$searchPattern, prompt: "Search Here")
                .toolbar(content: {
                    ToolbarItem(placement: .bottomBar) {
                        Text("Toolbar text")
                    }
                })
           }
   }
}

设置:

  • XCode 版本:13.4.1
  • 模拟器:iPhone 13

您可以使用 the.toolbar(.hidden, for: .bottomBar) 作为目标视图,如下面的代码所示:

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            NavigationLink {
                Text("Destination View")
            } label: {
                // hiding the toolbar for the destination view 
                Text("Root View").toolbar(.hidden, for: .bottomBar)
            }

        }.toolbar {
            ToolbarItem(placement: .bottomBar) {
                Text("Toolbar Text")
                    .background {
                        Color.gray
                    }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM