有没有办法改变视图的背景颜色DatePicker?我有以下选择器:
DatePicker(selection: $form.start, in: Date()..., displayedComponents: .date) {}
.padding(.vertical, 6)
.labelsHidden()
.accentColor(.black)
Run Code Online (Sandbox Code Playgroud)
但选择器在日期周围有灰色色调(见下图)。我只想整个背景都是白色的。我尝试过.background(Color.white),但这没有任何作用。如何让整个背景变成白色?
小智 8
我不同意更改默认的 DatePicker,特别是这种类型,因为您可能会破坏它未来的操作系统更新,而且您/您的设计师对它的愿景可能不如 Apple 愿景那么直观/易于访问。
不过,如果您需要像我一样这样做,以下是在以 iOS 14 为最低目标进行开发时的具体操作方法。
struct DatePickerView: View {
@State private var selectedDate = Date()
let dateFormatter = DateFormatter()
var body: some View {
HStack {
Text(dateFormatter.string(from: selectedDate))
.overlay {
DatePicker(
"",
selection: $selectedDate,
displayedComponents: .date
)
.blendMode(.destinationOver)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
我发现你可以通过如下初始化来做到这一点:
init() {
UIDatePicker.appearance().backgroundColor = UIColor.init(.white) // changes bg color
UIDatePicker.appearance().tintColor = UIColor.init(.white) // changes font color
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4216 次 |
| 最近记录: |