【问题标题】:How to change the line color of a UIDatePicker如何更改 UIDatePicker 的线条颜色
【发布时间】:2016-02-20 16:10:05
【问题描述】:

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

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

【问题讨论】:

  • 保持这样,看起来很性感:)

标签: ios swift datepicker styling uidatepicker


【解决方案1】:

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 开始,这不再起作用,正如某些 cmets 中所提到的,即查找子视图并获取它们有效,但设置背景颜色不再起作用。 我为此做了另一个肮脏的解决方案,我正在设置一个边框颜色有效的边框。

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

【讨论】:

  • 如果您删除较小的视图并添加您自己的作为替代,它也可以工作。
【解决方案2】:

首先,Apple 文档说,关于 Date Pickers,“您无法自定义日期选择器的外观。”

话虽如此,下面的代码正是您所需要的。我知道这不是最优雅的代码,但在这里

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

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

【讨论】:

  • 这是危险代码。它对子视图结构做出假设如果更改,代码将崩溃。
  • 感谢您提供此解决方案。但是有没有更好(非危险)的方法来为这些线条着色?
  • 在ios 10上测试,这个不行,你能确认一下吗?
  • @Bartu 这就是 rmaddy 对他/她的评论的意思:如果上面的代码发生变化,它将无法工作。 iOS 10 一定带来了一些破坏上述代码的变化。
  • @Bartu 看看下面我的解决方案。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    我不得不更改 datePicker 的高度约束,而这个 @kex 代码的 cus 对我不起作用。所以我修改了它:(适用于我在 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
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

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

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

      更安全的解决方案:

      extension UIView {
      
       func findViews<T: UIView>(subclassOf: T.Type) -> [T] {
          return recursiveSubviews.compactMap { $0 as? T }
       }
      
       var recursiveSubviews: [UIView] {
          return subviews + subviews.flatMap { $0.recursiveSubviews }
       }
      
      }
      
      self.datePicker.findViews(subclassOf: UIView.self)
                  .makeIterator()
                  .lazy
                  .filter({$0.bounds.height <= 1 && $0.bounds.width == self.datePicker.bounds.width})
                  .prefix(2)
                  .forEach({$0.backgroundColor = UIColor.red})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-14
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多