Search code examples
iosswifttextboxuipickerview

Using Swift, how do I disable the keyboard from popping up?


I am writing an application in Swift 3.0 using xCode.

When I press a textbox, I only want the picker view to show; so, why does the keyboard show with it? How can I disable it from popping up?

Here is the iPhone without touching the textbox: picture one

Here is the iPhone when I touch the textbox:

picture two

Here is my code for the view controller:

//the arrays for the height textbox
var heightInFeet = ["4\' 8\"", "4\' 9\"", "4\' 10\"", "4\' 11\"", "5\'", "5\' 1\"", "5\' 2\"", "5\' 3\"", "5\' 4\"", "5\' 5\"", "5\' 6\"", "5\' 7\"", "5\' 8\"", "5\' 9\"", "5\' 10\"", "5\' 11\"", "6\'", "6\' 1\"", "6\' 2\"", "6\' 3\"", "6\' 4\"", "6\' 5\"", "6\' 6\"", "6\' 7\"", "6\' 8\""]
var heightInMeters = ["1.42", "1.44", "1.46", "1.48", "1.5", "1.52", "1.54", "1.56", "1.58", "1.6", "1.62", "1.64", "1.66", "1.68", "1.7", "1.72", "1.74", "1.76", "1.78", "1.8", "1.82", "1.84", "1.86", "1.88", "2.0", "2.2"]
var pickerArray = [String]()

//the array for the activity level textbox
var activityLevel = ["None/Sedentary", "Light/Low", "Moderate/Active", "High/Very Active"]

...

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    var countrows : Int = pickerArray.count

    if pickerView == pickerViewForActivityLevel{
        countrows = self.activityLevel.count
    }
    return countrows
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == pickerViewForHeight{
        let titleRowForHeight = pickerArray[row]
        return titleRowForHeight
    }
    else if pickerView == pickerViewForActivityLevel{
        let titleRowForActivityLevel = activityLevel[row]
        return titleRowForActivityLevel
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == pickerViewForHeight{
        self.heightTextBox.text = self.pickerArray[row]
        self.pickerViewForHeight.isHidden = true
    }
    else if pickerView == pickerViewForActivityLevel{
        self.activityLevelTextBox.text = self.activityLevel[row]
        self.pickerViewForActivityLevel.isHidden = true
    }
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    if (textField == self.heightTextBox){
        self.pickerViewForHeight.isHidden = false
    }
    else if (textField == self.activityLevelTextBox){
        self.pickerViewForActivityLevel.isHidden = false
    }
}

@IBAction func heightSwitchAction(_ sender: UISwitch) {
    if heightUnitsSwitch.isOn{
        heightUnits.text = "Feet (ft)"
        pickerArray = []
        pickerArray.append(contentsOf: heightInFeet)
    }
    else{
        heightUnits.text = "Meters (m)"
        pickerArray = []
        pickerArray.append(contentsOf: heightInMeters)
    }
}

Thanks in advance.


Solution

  • Try to set inputView UIView() as an inputView textField.inputView = UIView()