Set text color and font for UIDatePicker in iOS8/Swift

后端 未结 16 2239
囚心锁ツ
囚心锁ツ 2020-12-03 21:02

I\'ve been having trouble trying to set the UIDatePicker font and color. Everything else in my app was fairly straightforward to adjust except

16条回答
  •  余生分开走
    2020-12-03 21:20

    I believe this is the definitive solution for countdown timers.
    It's an expansion of yildirimosman's answer.

    //text color
    datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
    //picker background
    datePicker.subviews[0].subviews[0].backgroundColor = UIColor.clearColor() //the picker's own background view
    //dividers
    datePicker.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
    datePicker.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
    //labels: "hours" and "min"
    datePicker.subviews[0].subviews[3].setValue(UIColor.lightGrayColor(), forKey: "textColor")
    datePicker.subviews[0].subviews[4].setValue(UIColor.lightGrayColor(), forKey: "textColor")
    //refresh the tableview (to force initial row textColor to change to white)
    datePicker.subviews[0].setNeedsLayout()
    datePicker.subviews[0].layoutIfNeeded()
    

提交回复
热议问题