簡體   English   中英

SwiftUI 列表頂部和底部的不同背景顏色

[英]SwiftUI Different background colors for the top and bottom of a List

我想為列表的頂部和底部制作不同的背景顏色。

當我使用 UIKIT 時,我喜歡這個鏈接

現在我使用 SwiftUI。 我不知道我怎么能做到這一點..

謝謝!

您可以將UIView添加到標題中。 我發現 400 的高度足以覆蓋下拉而不會看到頂部邊緣。

 var body: some View {
        
        NavigationView {
            
            List(data, id: \.self) { data in
                Text("\(data)")
            }
            .onAppear {
                let headerView = UIView(frame: CGRect(x: 0, y: -400, width: UIScreen.main.bounds.width, height: 400.0))
                headerView.backgroundColor = .lightGray
                UITableView.appearance().addSubview(headerView)
            }
            .navigationBarTitle("Title", displayMode: .inline)
        }
    }

在此處輸入圖片說明


如果您想要列表下方的不同背景顏色,請添加另一個UIView以更改 backgroundView:

let backgroundView = UIView()
backgroundView.backgroundColor = .black
UITableView.appearance().backgroundView = backgroundView

在此處輸入圖片說明

暫無
暫無

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

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