简体   繁体   English

SwiftUI - 点击 NavigationLink 后底栏为空

[英]SwiftUI - Empty bottom bar after tapping NavigationLink

I have a NavigationView with a toolbar that contains a ToolBarItem with a .bottomBar placement and a search field.我有一个带有toolbarNavigationView ,其中包含一个带有ToolBarItem位置的.bottomBar和一个搜索字段。 This NavigationView contains a ScrollView with content that exceeds the screen's vertical size, which means that the bottom bar has a background, as seen below:这个NavigationView包含一个ScrollView ,其内容超出了屏幕的垂直尺寸,这意味着底部栏有背景,如下所示:

根视图截图

When the user taps the "Root View" text element they navigate to a new view, in this case, just another text displaying "Detail View".当用户点击“Root View”文本元素时,他们导航到一个新视图,在本例中,只是另一个显示“Detail View”的文本。 The problem, however, is that the bottom toolbar's background remains in the screen instead of vanishing as expected.然而,问题是底部工具栏的背景保留在屏幕中,而不是像预期的那样消失。 See the screenshot below:请参见下面的屏幕截图:

在此处输入图像描述

This behavior is not seen if I remove the search bar or shrink the ScrollView 's height to fit the vertical dimension of the device.如果我删除搜索栏或缩小ScrollView的高度以适合设备的垂直尺寸,则不会看到此行为。 I tried googling this issue to see if it was a known bug with a workaround but maybe I'm not searching the right keywords.我试着用谷歌搜索这个问题,看看它是否是一个有解决方法的已知错误,但也许我没有搜索正确的关键字。 How can I fix this issue?我该如何解决这个问题?

Please see the bare minimum to replicate the issue below:请查看最低限度以复制以下问题:

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")
                    }
                })
           }
   }
}

Setup:设置:

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

You can use the.toolbar(.hidden, for: .bottomBar) for the destination view as shown in the code below:您可以使用 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