SwiftUI:列表上的渐变背景

2024-01-19

有人可以告诉我如何在 SwiftUI 列表上添加渐变背景吗?

当前代码:

struct TestView: View {
    var body: some View {
        LinearGradient(gradient: Gradient(colors: [Color.red, Color.purple]), startPoint: .top, endPoint: .bottom)
            .edgesIgnoringSafeArea(.vertical)
            .overlay(
                List {
                    Group {
                        Text("Hallo")
                        Text("World")
                    }
                    .background(Color.blue)
                }
                .background(Color.green)
                .padding(50)
        )
    }
}

当前布局,其中渐变在单元格后面可见。我想让单元格透明以看到下面的渐变。

Thanks!


您已经添加了渐变。只需使背景颜色清晰并去掉那些测试代码即可;)

iOS 16

您可以使用隐藏任何可滚动内容的背景scrollContentBackground(.hidden)修饰语

iOS 15 及以下版本

您应该隐藏默认列表背景和所有单元格使用appearance的财产UITableView and UITableViewCell

您也可以使用隐藏单元格背景.listRowBackground(.clear)

Example

struct TestView: View {

    init() {
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        LinearGradient(gradient: Gradient(colors: [Color.red, Color.purple]), startPoint: .top, endPoint: .bottom)
            .edgesIgnoringSafeArea(.vertical)
            .overlay(
                List {
                    Group {
                        Text("Hallo")
                        Text("World")
                    }
                }
                .padding(50)
        )
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SwiftUI:列表上的渐变背景 的相关文章

随机推荐