Ios 将inputView背景颜色从模糊灰色更改为其他颜色
我正在尝试将Ios 将inputView背景颜色从模糊灰色更改为其他颜色,ios,swift,uipickerview,Ios,Swift,Uipickerview,我正在尝试将inputView背景色从默认的浅灰色更改为其他颜色,但是我看不到任何解决方案 我有一个UIPickerView,当用户触摸textField时会打开它,它就像底部的键盘一样inputView。我是这样实现的:myTextField.inputView=myPickerView 由于我在inputView中使用它,我想我实际上必须更改它的背景颜色,但不知道如何更改 看起来是这样的: 现在我想改变它的背景色 我已经试过了: myPickerView.setValue(0.8,forK
inputView
背景色从默认的浅灰色更改为其他颜色,但是我看不到任何解决方案
我有一个UIPickerView,当用户触摸textField
时会打开它,它就像底部的键盘一样inputView
。我是这样实现的:myTextField.inputView=myPickerView
由于我在inputView中使用它,我想我实际上必须更改它的背景颜色,但不知道如何更改
看起来是这样的:
现在我想改变它的背景色
我已经试过了:
myPickerView.setValue(0.8,forKey:“alpha”)
myPckerView.backgroundColor=UIColor.blue
self.inputView.backgroundColor=UIColor.blue
mytextfield.inputView?.tintColor = UIColor.green
或者也试试这个
mytextfield.inputView?.tintColor = UIColor.clear
mytextfield.inputView?.backgroundColor = UIColor.blue
好的-由于您可以在“正常”视图中获得所需的
UIPickerView
外观,因此您可以创建一个“正常”视图用作.inputView
,然后将选择器视图添加为子视图
下面是一个简单的示例-只需在IB中添加一个UITextField
,并将其连接到IBOutlet
:
class MyViewController: UIViewController
{
@IBOutlet weak var theTextField: UITextField!
let cancelButton: UIButton = {
let b = UIButton()
b.setTitle("Cancel", for: .normal)
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let doneButton: UIButton = {
let b = UIButton()
b.setTitle("Done", for: .normal)
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let pvToolbar: UIView = {
let v = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 40))
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .black
return v
}()
let pvBackground: UIView = {
let v = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
v.backgroundColor = .white
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let pickerView: UIPickerView = {
let p = UIPickerView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
p.showsSelectionIndicator = true
p.translatesAutoresizingMaskIntoConstraints = false
return p
}()
override func viewDidLoad() {
super.viewDidLoad()
// add buttons to the inputAccessoryView "toolbar"
pvToolbar.addSubview(cancelButton)
pvToolbar.addSubview(doneButton)
cancelButton.leftAnchor.constraint(equalTo: pvToolbar.leftAnchor, constant: 8.0).isActive = true
cancelButton.topAnchor.constraint(equalTo: pvToolbar.topAnchor, constant: 6.0).isActive = true
cancelButton.bottomAnchor.constraint(equalTo: pvToolbar.bottomAnchor, constant: -6.0).isActive = true
doneButton.rightAnchor.constraint(equalTo: pvToolbar.rightAnchor, constant: -8.0).isActive = true
doneButton.centerYAnchor.constraint(equalTo: cancelButton.centerYAnchor).isActive = true
// add pickerView to our plain UIView that will become our inputView
pvBackground.addSubview(pickerView)
pickerView.topAnchor.constraint(equalTo: pvBackground.topAnchor).isActive = true
pickerView.bottomAnchor.constraint(equalTo: pvBackground.bottomAnchor).isActive = true
pickerView.centerXAnchor.constraint(equalTo: pvBackground.centerXAnchor).isActive = true
pickerView.widthAnchor.constraint(equalTo: pvBackground.widthAnchor, multiplier: 1.0).isActive = true
pickerView.delegate = self
pickerView.dataSource = self
// pvBackground "contains" the actual pickerView
theTextField.inputView = pvBackground
theTextField.inputAccessoryView = pvToolbar
}
}
extension AnimConstraintsViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "Row: \(row)"
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 30
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
// simple label with centered text as our viewForRow
let label = UILabel()
label.backgroundColor = .white
label.textColor = .black
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 17.0, weight: UIFontWeightRegular)
label.text = "Row: \(row)"
return label
}
}
结果:
您是否尝试过使用
myTextField.inputView.backgroundColor=UIColor.blue
。什么是self.inputView?@Subramanian是的,我也试过了。根据文档,当对象成为第一响应者时,“self.inputView
被”//调用并显示。进入响应者链。”。基本上,它用UIPickerView
@TarvoMäesepp替换了键盘-你希望它看起来怎么样?这是使用UIPickerView
作为inputView
的一个示例(我怀疑您是否想要这些颜色):@DonMag我希望它看起来平坦。没有你的前一层是蓝色的在我的图像。我甚至不知道滚动效果。类似于@TarvoMäesepp的东西-你能在你的普通视图中(而不是作为inputView的一部分)在UIPickerView中获得你想要的外观吗?你太棒了。为什么我没有想到这个主意。正是我想要的。