簡體   English   中英

在 SwiftUI 中更改具有 NavigationView 和 ScrollView 的 TabView 內視圖的背景顏色

[英]Change background color of View inside TabView having NavigationView and ScrollView in SwiftUI

我想構建一個包含 4 個選項卡的TabView ,其中包含集合視圖。 下面是我的一個名為“畫廊”的標簽的代碼。

 var body: some View {
        NavigationView {
            ScrollView {
                GridStack(rows: 3, columns: 2) { row, column, totalColumn in
                    CardView(card: self.cards[(row * totalColumn) + column])
                }.padding().background(Color.red)
            }
            .navigationBarTitle("Gallery")
        }
    }

當我為ScrollView提供背景顏色時,滾動不適用於NavigationView largeTitle。 我怎樣才能做到這一點,我想為全視圖的背景賦予紅色? 如果我需要為所有選項卡實現相同的背景顏色怎么辦?

在此處輸入圖像描述

這是可能的方法(在這種情況下滾動視圖不會被破壞)

NavigationView {
    GeometryReader { gp in
        ScrollView {
            ZStack(alignment: .top) {
                Rectangle().fill(Color.red) // << background

              // ... your content here, internal alignment might be needed

            }.frame(minHeight: gp.size.height)

        }
        .navigationBarTitle("Gallery")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM