SwiftUI列表背景色

时间:2019-10-28 13:31:00

标签: swiftui ios13 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)
}
}

该视图托管在混合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))
}
}

我尝试了可以​​想到的.background.listRowBackground(Color.black).colorMultiply(.black)的任意组合,而我得到的最好的就是这个

issue

1 个答案:

答案 0 :(得分:5)

在iOS中,所有SwiftUI的List都由UITableView支持。因此您需要更改 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)
    }
}

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

请注意,这些顶部和底部白色区域是安全区域,您可以使用.edgesIgnoringSafeArea()修饰符删除它们。

相关问题