How to change the line color of a UIDatePicker

梦想的初衷 提交于 2020-01-01 09:55:33

问题


I have got a UIDatePicker in one of my ViewControllers. As you can see, the background is dark.

What i already managed to change is the text color to white. What i just can't change is the color of the two lines above and below the selected date. It always remains default dark grey. Does anyone have a code snipped to achieve coloring these lines?


回答1:


To start with, Apple docs says, about Date Pickers, "You cannot customize the appearance of date pickers."

That being said, the following code does exactly what you need. I understand it's not the most elegant piece of code, but here it is

datePicker.subviews[0].subviews[1].backgroundColor = UIColor.redColor()
datePicker.subviews[0].subviews[2].backgroundColor = UIColor.redColor()

UIDatePicker has a subview, UIDatePickerView, which has 3 subviews, 2 of the latter of which are the two lines that indicate the selection row.




回答2:


huniser_suraj answer is correct, just use additional check for future changes, this is how I use it, for UIPickerView & UIDatePicker, respectively:

    for subview in self.picker.subviews {

        if subview.frame.height == 1 {

            subview.backgroundColor = UIColor.whiteColor()
        }
    }

    if let pickerView = self.datePicker.subviews.first {

        for subview in pickerView.subviews {

            if subview.frame.height == 1 {

                subview.backgroundColor = UIColor.whiteColor()
            }
        }
    }

iOS 10 UPDATE

From iOS 10 on, this doesn't work anymore, as mentioned in some of the comments, i.e. finding the subviews & getting to them works, but setting the background color doesn't work anymore. I did another dirty solution for this, I'm setting a border with a border color which works.

    for subview in self.picker.subviews {

        if subview.frame.height <= 5 {

            subview.backgroundColor = UIColor.white
            subview.tintColor = UIColor.white
            subview.layer.borderColor = UIColor.white.cgColor
            subview.layer.borderWidth = 0.5            }
    }

    if let pickerView = self.datePicker.subviews.first {

        for subview in pickerView.subviews {

            if subview.frame.height <= 5 {

                subview.backgroundColor = UIColor.white
                subview.tintColor = UIColor.white
                subview.layer.borderColor = UIColor.white.cgColor
                subview.layer.borderWidth = 0.5
            }
        }
        self.datePicker.setValue(UIColor.white, forKey: "textColor")
    }



回答3:


It can be done in a pretty simple way with IB. Just put 2 UiView with height=1, equal width as UIDatePicker. Then center it with UIDatePicker vertically and horizontally. For upper line, set vertical offset to 18, and the other one set to -18. Done!




回答4:


I had to change height constraint for datePicker and cus of this @kex code didn't worked for me. so I modified it: (works for me on swift 5 and iOS 12)

if let _pickerView = self.datePicker.subviews.first {
    for _subview in _pickerView.subviews {
        if (_subview.backgroundColor != nil) {
            _subview.backgroundColor = UIColor.white
            _subview.tintColor = UIColor.white
            _subview.layer.borderColor = UIColor.white.cgColor
            _subview.layer.borderWidth = 1
        }
    }
}


来源:https://stackoverflow.com/questions/33788274/how-to-change-the-line-color-of-a-uidatepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!