如何在特定文本字段上禁用键盘但允许选择器视图作为输入 Swift?

How to disable keyboard on specific textField but allow picker view as input Swift?

我在视图控制器中有 2 个文本字段,一个使用 pickerView 作为输入,另一个使用键盘,问题是键盘在 pickerView 下仍然启用。我已经设置好,当使用底部的文本字段时,键盘会向上移动视图以使文本字段可见,但是当我单击使用 pickerView 的顶部文本字段时,它也会向上移动视图,您看不到该文本字段上的输入。我只需要在顶部 textField 上使用 pickerView,以便在使用该文本字段时视图不会向上移动。 这是我用来向上移动视图的方法:

func keyboardWillShow(notification: NSNotification) {

    bottomConstraint.constant = 200
    topConstraint.constant = -200
    UIView.animateWithDuration(0.3) {
        self.view.layoutIfNeeded()
    }
}



func keyboardWillHide(notification: NSNotification) {

    bottomConstraint.constant = 8
    topConstraint.constant = 10
    UIView.animateWithDuration(0.3) {
        self.view.layoutIfNeeded()
    }
}

这是我在 viewDidLoad 中对 pickerView 的设置:(colorCodePicker 是将 pickerView 作为输入的文本字段)

let pickerView = UIPickerView()

    pickerView.delegate = self

    pickerView.backgroundColor = UIColor.whiteColor()

    colorCodePicker.inputView = pickerView

我已经尝试了这里的大部分解决方案,但没有任何效果,即如果我禁用用户输入,那么我就无法调用 pickerView,还尝试在 textField 上插入一个清除按钮,就像建议之一一样没有运气。或者可能是仅在底部文本字段处于活动状态时向上移动视图的解决方案,有什么想法吗?

我知道这不是确切的答案,但如果您只使用 IQKeyboardManager,则无需在应用程序的任何场景中考虑这些员工。

IQKeyboardManager

keyboardWillShowkeyboardWillHide 中检查 if colorCodePicker.isFirstResponder() 以决定是否更改约束。

可以通过快速修复来挽救局面,但我建议使用 UITableViewController 构建表单页面。它会自动进行键盘处理。是的,您需要创建包含 UITextFieldUITableViewCell,但我认为这是可行的。

我不喜欢使用独立 UITableViewController,因为它们没有 UIView 作为它们的 self.view,因此我将它们嵌入我的视图控制器并访问它的 tableView 在 segue 期间。即使它不是 tableViewdataSource/delegate,它也会处理键盘。