gpt4 book ai didi

ios - 如何更改 UIDatePicker 的背景颜色?在 swift 3.0

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:39 25 4
gpt4 key购买 nike

我正在为文本字段上的键盘添加一个日期选择器。我可以更改 datePicker 文本颜色甚至它的 alpha,但我无法更改它的 backgroundColor,即使该选项已提供给我。

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

有什么想法吗?

这是我的代码:

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

编辑

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

import UIKit

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

最佳答案

它似乎在起作用,但不一定以您想要的方式起作用。

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

更改 "textColor" 属性也会产生一些明显的变化,但控件似乎是根据背景动态计算准确的前景色,这种方式始终清晰可辨。如果您尝试使用 .ligtGray.black 等颜色,这将变得很明显。

抱歉,要进行进一步的定制,您运气不好。 :(

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

Screenshot of date picker

关于ios - 如何更改 UIDatePicker 的背景颜色?在 swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40476947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com