Swift DatePickerのデフォルトの背景色を変えるサンプル
環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
構文
//initメソッドやonAppearなどで背景色を代入
UIDatePicker.appearance().backgroundColor = color
「UIDatePicker.appearance().backgroundColor」に代入した色が、DatePickerの背景色になります。
使用例
struct ContentView: View {
@State var date = Date()
init () {
UIDatePicker.appearance().backgroundColor = .green.withAlphaComponent(0.3)
}
var body: some View {
VStack{
DatePicker("Test", selection: $date)
.datePickerStyle(GraphicalDatePickerStyle())
.padding()
}
}
}
struct ContentView: View {
@State var date = Date()
init () {
UIDatePicker.appearance().backgroundColor = .green.withAlphaComponent(0.3)
}
var body: some View {
VStack{
DatePicker("Test", selection: $date)
.datePickerStyle(GraphicalDatePickerStyle())
.padding()
}
}
}
struct ContentView: View { @State var date = Date() init () { UIDatePicker.appearance().backgroundColor = .green.withAlphaComponent(0.3) } var body: some View { VStack{ DatePicker("Test", selection: $date) .datePickerStyle(GraphicalDatePickerStyle()) .padding() } } }