gpt4 book ai didi

ios - 将 inputView 背景颜色从模糊灰色更改为其他任何颜色

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:08 33 4
gpt4 key购买 nike

我正在尝试将 inputView 背景颜色从默认的灰色更改为其他颜色,但我没有看到任何解决方案。

我有 UIPickerView,它会在用户触摸 textField 时打开,它像键盘一样位于底部,如 inputView。我是这样实现的:myTextField.inputView = myPickerView

因为我在 inputView 中使用它,我想我实际上必须更改它的背景颜色,但不知道如何更改。

看起来像这样:

enter image description here

现在我想改变它的背景颜色。

我已经试过了:

  1. myPickerView.setValue(0.8, forKey: "alpha")
  2. myPckerView.backgroundColor = UIColor.blue
  3. self.inputView.backgroundColor = UIColor.blue
  4. 以及来自 SO 的几乎所有解决方案

这是输出(上面有一些令人讨厌的模糊层):

enter image description here

我是否必须为此创建某种自定义键盘,或者是否有解决方法,或者 Apple 再次告诉您您可以做什么,什么不能像机器人一样?

最佳答案

好的 - 因为您可以在“普通” View 中以您想要的方式获得 UIPickerView 外观,所以您可以创建一个“普通” View 以用作您的.inputView 然后将您的选择器 View 添加为 subview 。

这是一个简单的示例 - 只需在 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
}
}

结果:

enter image description here

关于ios - 将 inputView 背景颜色从模糊灰色更改为其他任何颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45085104/

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