将inputView背景颜色从模糊灰色更改为其他颜色

5

我想要将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

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


你尝试过使用 myTextField.inputView.backgroundColor = UIColor.blue 吗?那么 self.inputView 是什么? - Subramanian P
@Subramanian 是的, 我也尝试过那种方法。根据文档,self.inputView是"// Called and presented when object becomes first responder. Goes up the responder chain.",它会用UIPickerView替换键盘。 - Tarvo Mäesepp
@TarvoMäesepp - 你希望它看起来怎样?这是使用UIPickerView作为inputView的一个示例(我怀疑你不想要这些颜色):http://imgur.com/a/dk94Y - DonMag
@DonMag 我想让它看起来扁平化,不要前面的那一层,我的图片中是蓝色的。我甚至不想要滚动效果。类似于这个 https://i.stack.imgur.com/kKWxa.png - Tarvo Mäesepp
@TarvoMäesepp - 你能在普通视图中(而不是作为inputView的一部分)获得UIPickerView所需的外观吗? - DonMag
@DonMag 是的,我可以。 - Tarvo Mäesepp
2个回答

2

好的 - 既然你可以在“正常”视图中按照自己的意愿获取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
    }
}

结果:

在这里输入图片描述


(注:此内容为HTML代码,已翻译为中文,保留了HTML标签)

你太棒了。为什么我没想到这个主意。正是我一直在寻找的东西。 - Tarvo Mäesepp

1
使用这段代码。
mytextfield.inputView?.tintColor = UIColor.green

或者也可以尝试这个。
mytextfield.inputView?.tintColor = UIColor.clear
mytextfield.inputView?.backgroundColor = UIColor.blue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接