簡體   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