如何更改 UIDatePicker 的线条颜色

How to change the line color of a UIDatePicker

我的一个 ViewController 中有一个 UIDatePicker。如您所见,背景是黑暗的。

我已经设法将文本颜色更改为白色。我不能改变的是所选日期上方和下方两条线的颜色。它始终保持默认的深灰色。有没有人剪下代码来为这些线条着色?

首先,Apple 文档说,关于 Date Pickers、"You cannot customize the appearance of date pickers."

话虽这么说,但下面的代码正是您所需要的。我知道这不是最优雅的代码,但它是

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

UIDatePicker有一个子视图UIDatePickerView,它有3个子视图,其中2个子视图是表示选择行的两行。

huniser_suraj 答案是正确的,只是对未来的变化使用额外的检查,这就是我使用它的方式,分别用于 UIPickerView 和 UIDatePicker:

    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 次更新

从 iOS 10 开始,这不再有效,如某些评论中所述,即找到子视图并找到它们有效,但设置背景颜色不再有效。 我为此做了另一个肮脏的解决方案,我正在设置一个带有有效边框颜色的边框。

    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")
    }

这可以通过 IB 以非常简单的方式完成。 只需放置 2 个高度为 1、宽度与 UIDatePicker 相等的 UiView。 然后用 UIDatePicker 垂直和水平居中。对于上一行,将垂直偏移设置为 18,将另一行设置为 -18。 完成!

我不得不更改 datePicker 的高度限制,因为这个 @kex 代码对我不起作用。所以我修改了它:(适用于 swift 5 和 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
        }
    }
}

是的,这是一种不安全的方法。但作为一种选择,为什么不呢。

 if #available(iOS 13.0, *) {
    datePicker.setValue(UIColor.red, forKey: "magnifierLineColor")
    }

更安全的解决方案:

extension UIView {

 func findViews<T: UIView>(subclassOf: T.Type) -> [T] {
    return recursiveSubviews.compactMap { [=11=] as? T }
 }

 var recursiveSubviews: [UIView] {
    return subviews + subviews.flatMap { [=11=].recursiveSubviews }
 }

}

self.datePicker.findViews(subclassOf: UIView.self)
            .makeIterator()
            .lazy
            .filter({[=11=].bounds.height <= 1 && [=11=].bounds.width == self.datePicker.bounds.width})
            .prefix(2)
            .forEach({[=11=].backgroundColor = UIColor.red})