SwiftUI列表背景色

Ale*_*tiş 1 ios13 swiftui xcode11 swiftui-list

我正在尝试使用以下代码将视图背景色设置为黑色

struct RuleList: View {[![enter image description here][1]][1]
private var presenter: ConfigurationPresenter?

@ObservedObject
private var viewModel: RowListViewModel

init(presenter: ConfigurationPresenter?, viewModel: RowListViewModel) {
    self.presenter = presenter
    self.viewModel = viewModel
}

var body: some View {
    List(viewModel.configurations) { configuration in
        RuleListRow(website: configuration.website).background(Color.black)
    }.background(Color.black)
}
}

struct RuleListRow: View {
var website: Website
@State private var websiteState = 0

var body: some View {
    VStack {
        Text(website.id).foregroundColor(.white)

        Picker(website.id, selection: $websiteState) {
            Text("Permis").tag(0)
            Text("Ascuns").tag(1)
            Text("Blocat").tag(2)
        }.pickerStyle(SegmentedPickerStyle()).background(Color.crimson)
    }.listRowBackground(Color.green)
}
}
Run Code Online (Sandbox Code Playgroud)

该视图托管在混合UIKit-SwiftUI故事板中,因此此特定视图嵌入在Hosting控制器中

class ConfigurationHostingController: UIHostingController<RuleList> {
private var presenter: ConfigurationPresenter = ConfigurationPresenter()

required init?(coder: NSCoder) {
    super.init(rootView: RuleList(presenter: presenter, viewModel: presenter.rowListViewModel))
}
}
Run Code Online (Sandbox Code Playgroud)

我试过的任意组合.background.listRowBackground(Color.black).colorMultiply(.black)我能想到的,我得到了最好的是这

问题

Moj*_*ini 5

所有SwiftUI List都由UITableViewiOS中的a支持。因此,您需要更改tableView的背景颜色。但是由于ColorUIColor值略有不同,因此您可以摆脱UIColor

struct ContentView: View {

    init() {
        UITableView.appearance().backgroundColor = .clear // tableview background
        UITableViewCell.appearance().backgroundColor = .clear // cell background
    }

    var body: some View {
        List {
            ,,,
        }
        .background(Color.yellow)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用所需的任何背景(包括所有Color

请注意,顶部和底部的白色区域是安全的,您可以使用.edgesIgnoringSafeArea()修饰符来摆脱它们。

  • 就是这样 我试过只为表格设置它,但是显然单元格也需要它。 (2认同)