【问题标题】:How to change the UIDatePicker backgroundColor? in Swift 3.0如何更改 UIDatePicker 背景颜色?在斯威夫特 3.0
【发布时间】:2016-11-08 00:20:41
【问题描述】:

我在文本字段上为我的键盘添加一个 datePicker。 我可以更改 datePicker 文本颜色甚至它的 alpha,但我无法更改它的 backgroundColor,即使给了我选项。

我知道 UIDatePicker 不是很可定制,但由于给了我一个 backgroundColor 选项,我很惊讶为什么它没有改变。

有什么想法吗?

这是我的代码:

    datePickerView.backgroundColor = .clear
    datePickerView.setValue(backgroundTint, forKeyPath: "textColor")
    datePickerView.datePickerMode = UIDatePickerMode.date
    inputView.addSubview(datePickerView)

编辑

我在 UITableViewCell 的子类中编写代码 --->

导入 UIKit

类 AgeTableViewCell: UITableViewCell, UITextFieldDelegate {

let maxLength: Int = 10
let ageTextField = ProfileNameUITextField()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    self.contentView.addSubview(ageTextField)
    
    self.contentView.backgroundColor = .clear

    if self.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        self.separatorInset = UIEdgeInsets.zero
    }
    if self.responds(to:#selector(setter: UIView.layoutMargins)) {
        self.layoutMargins = UIEdgeInsets.zero
    }
    if self.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
        self.preservesSuperviewLayoutMargins = false
    }
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

override func layoutSubviews() {
    super.layoutSubviews()
    
    let width = self.bounds.width
    let height = self.bounds.height
    let borderWidth: CGFloat = 1.0
    let color = UIColor.white.withAlphaComponent(0.1)
    ageTextField.frame = CGRect(x: 0.0, y: borderWidth, width: width - borderWidth, height: height - 2 * borderWidth)
    ageTextField.borderStyle = .none
    ageTextField.backgroundColor = color
    ageTextField.textColor = .white
    ageTextField.tintColor = .white
    ageTextField.delegate = self
    ageTextField.attributedPlaceholder = NSAttributedString(string:"Birth Date",
                                                            attributes:[NSForegroundColorAttributeName: UIColor.lightGray])
    setUpAgeTextFieldsInputView()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let oldLength: Int = (textField.text?.characters.count)!
    let replacementLength: Int = string.characters.count
    let rangeLength: Int
        = Int(range.length)
    let newLength = oldLength - rangeLength + replacementLength
    let returnKey = (string as NSString).range(of: "\n").location != NSNotFound
    return newLength <= maxLength || returnKey
}

func setUpAgeTextFieldsInputView() {
    let inputView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: self.bounds.width, height: 140))
    
    inputView.backgroundColor = backgroundTint
    
    // setup date picker
    let datePickerView = UIDatePicker(frame: CGRect(x: 0.0, y: 40.0, width: self.bounds.width, height: 100))
    datePickerView.backgroundColor = .clear
    datePickerView.setValue(backgroundTint, forKeyPath: "textColor")
    datePickerView.datePickerMode = UIDatePickerMode.date
    
    // set the date picker max date and showing date
    let gregorian = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
    let currentDate = NSDate()
    let component = NSDateComponents() ; component.year = -18
    let date = gregorian.date(byAdding: component as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))
    datePickerView.date = date!
    datePickerView.maximumDate = currentDate as Date
    
    inputView.addSubview(datePickerView)
    
    // Make done button
    let doneButton = UIButton(frame: CGRect(x: 0.0, y: 1.0, width: self.bounds.width, height: 38.0))
    doneButton.backgroundColor = UIColor.white.withAlphaComponent(0.1)
    doneButton.setTitle("Done", for: UIControlState.normal)
    doneButton.setTitleColor(appPink, for: UIControlState.normal)
    doneButton.setTitleColor(UIColor.gray, for: UIControlState.highlighted)
    
    inputView.addSubview(doneButton)
    
    // set actions for buttons and picker
    doneButton.addTarget(self, action: #selector(done), for: UIControlEvents.touchUpInside)
    ageTextField.inputView = inputView
    datePickerView.addTarget(self, action: #selector(handleDatePicker(_:)), for: UIControlEvents.valueChanged)
}

func done() {
    self.endEditing(true)
}

func handleDatePicker(_ sender: UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    ageTextField.text = dateFormatter.string(from: sender.date)
}

}

【问题讨论】:

  • datePickerView.backgroundColor = UIColor.blue
  • datePickerView.backgroundColor = .yellow 或任何其他颜色作品。您必须为您的问题提供更多背景信息。

标签: ios swift uidatepicker


【解决方案1】:

它似乎在起作用,只是不一定按照你想要的方式。

例如,将backgroundColor 更改为引人注目的内容(.clear 除外)显然有效。它将色调更改为该颜色。

更改"textColor" 属性也会产生一些明显的变化,但似乎控件正在根据背景动态计算准确的前景色,使其始终清晰易读。如果您尝试使用 .ligtGray.black 这样的颜色,这将变得显而易见。

抱歉,如果您想进一步定制,那您就不走运了。 :(

picker.backgroundColor = .white
picker.setValue(UIColor.red, forKey:"textColor")

【讨论】:

    【解决方案2】:

    您可能需要设置 UIKeyboardType = .default 和 UIKeyboardAppearance = .default

    【讨论】:

      【解决方案3】:

      Swift 5+ 答案

      picker.setValue(UIColor.lightGray, forKey: "backgroundColor")
      

      为我工作!!!

      【讨论】:

        【解决方案4】:

        嘿,通过我的代码后,我意识到我一直在为我的文本字段分配一个键盘类型,然后我试图将其输入视图更改为日期选择器视图。删除该分配解决了我的 bgcolor 问题。

        【讨论】:

          猜你喜欢
          • 2016-06-01
          • 2017-07-22
          • 2016-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-01
          • 2017-12-08
          相关资源
          最近更新 更多