如何更改UIDatePicker backgroundColor?Swift 3.0

4xrmg8kj  于 2023-05-16  发布在  Swift
关注(0)|答案(5)|浏览(195)

我正在为文本字段上的键盘添加日期选择器。我可以改变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)
}

}

8tntrjer

8tntrjer1#

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

qyswt5oh

qyswt5oh2#

这对我很有效

datePicker.subviews[0].backgroundColor = .red
3wabscal

3wabscal3#

它似乎在起作用,只是不一定以你想要的方式。
例如,将backgroundColor更改为值得注意的东西(而不是.clear)显然有效。它会将色调更改为该颜色。
更改"textColor"属性也会产生一些明显的变化,但控件似乎是根据背景动态计算准确的前景色,其方式始终清晰可辨。如果你使用像.ligtGray.black这样的颜色,这一点就会变得很明显。
对不起,对于进一步的定制,你运气不好。:(

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

yshpjwxd

yshpjwxd4#

Swift 5+ Answer

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

为我工作!!!

r3i60tvu

r3i60tvu5#

您可能希望设置UIKeyboardType = .default和UIKeyboardAppearance = .default

相关问题