使用Swift,如何禁用键盘弹出?

时间:2017-09-19 02:40:06

标签: ios swift textbox uipickerview

我正在使用xCode在Swift 3.0中编写应用程序。

当我按下文本框时,我只希望显示选择器视图;所以,为什么键盘显示它呢? 如何禁止它弹出?

这是没有触摸文本框的iPhone: picture one

当我触摸文本框时,这是iPhone:

picture two

这是我的视图控制器代码:

//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)
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

尝试将inputView UIView()设置为inputView textField.inputView = UIView()

答案 1 :(得分:0)

如果此inputView中的值为nil,则文本字段在成为第一响应者时显示标准系统键盘。为此custom view分配inputView会导致显示该视图。 此属性的默认值为nil.so您可以使用inputView来显示选择器。

 let picker : UIDatePicker = UIDatePicker()
 textField.inputView = picker