删除/更改 SwiftUI 列表中的部分标题背景颜色 [英] Remove/change section header background color in SwiftUI List

查看:40
本文介绍了删除/更改 SwiftUI 列表中的部分标题背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SwiftUI 中的简单 List,如何更改/删除部分标题的标准背景色

With a simple List in SwiftUI, how do I change/remove the standard background color for the section header

struct ContentView : View {
    var body: some View {
        List {
            ForEach(0...3) { section in
                Section(header: Text("Section")) {
                    ForEach(0...3) { row in
                        Text("Row")
                    }
                }
            }
        }
    }
}

推荐答案

在 beta 4 中,relativeWidth 已弃用.已更新代码以反映这一点.

不幸的是,没有设置背景颜色的快速参数.但是,您仍然可以这样做:

Unfortunately, there's no quick parameter to set the background color. However, you can still do it:

import SwiftUI

struct ContentView : View {
    var body: some View {
        List {
            ForEach(0...3) { section in
                Section(header:
                            CustomHeader(
                                name: "Section Name",
                                color: Color.yellow
                            )
                        ) {
                    ForEach(0...3) { row in
                        Text("Row")
                    }
                }
            }
        }
    }
}

struct CustomHeader: View {
    let name: String
    let color: Color

    var body: some View {
        VStack {
            Spacer()
            HStack {
                Text(name)
                Spacer()
            }
            Spacer()
        }
        .padding(0).background(FillAll(color: color))
    }
}

struct FillAll: View {
    let color: Color
    
    var body: some View {
        GeometryReader { proxy in
            self.color.frame(width: proxy.size.width * 1.3).fixedSize()
        }
    }
}

这篇关于删除/更改 SwiftUI 列表中的部分标题背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆