2017-07-13 53 views
2

我想將inputView背景顏色從默認灰色更改爲其他顏色,但我沒有看到任何解決方案。將inputView背景顏色從模糊灰色更改爲其他任何內容

我有UIPickerView,當用戶觸摸textField時打開,它的底部像鍵盤inputView。我是這樣實現的:myTextField.inputView = myPickerView

由於我在inputView中使用它,我想我實際上必須改變它的backgroud顏色,但不知道如何。

它看起來像這樣:

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

我必須創建某種自定義的鍵盤只是針對或有一種解決方法或蘋果再次告訴你什麼可以做,什麼不喜歡的機器人?

+0

做你試着用'myTextField.inputView.backgroundColor = UIColor.blue'。什麼是'self.inputView'? – Subramanian

+0

@Subramanian是的,我也試過。 'self.inputView'是「//當對象成爲第一響應者時被調用並呈現,然後進入響應者鏈。」根據文件。基本上,它用'UIPickerView'取代了鍵盤。 –

+0

@TarvoMäesepp - 你想要怎麼樣?這是使用'UIPickerView'作爲'inputView'的一個例子(我懷疑你想要這些顏色):http://imgur.com/a/dk94Y – DonMag

回答

2

OK - 因爲你可以得到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 
    } 
} 

結果:

enter image description here

+0

你太棒了。爲什麼我沒有想到這個想法。正是我在找什麼。 –

0

使用此代碼

mytextfield.inputView?.tintColor = UIColor.green 

或嘗試這一個也

mytextfield.inputView?.tintColor = UIColor.clear 
mytextfield.inputView?.backgroundColor = UIColor.blue 
+0

它也沒有工作。輸出完全相同。 –

相關問題