简体   繁体   English

如何禁用特定textField上的键盘但允许选择器视图作为输入Swift?

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

I have 2 textFields in a view controller one uses a pickerView as input and the other uses a keyboard, the problem is the keyboard is still enabled under the pickerView. 我在视图控制器中有2个textFields,一个使用pickerView作为输入,另一个使用键盘,问题是在pickerView下仍然启用了键盘。 I have it set up that when the textfield in the bottom is being used the keyboard moves the view up to make the textField visible but when i click on the top textField which uses the pickerView it also moves the view up and you can't see the input on that textField. 我设置了当使用底部的文本字段时,键盘向上移动视图以使textField可见但是当我点击使用pickerView的顶部textField时它也会向上移动视图而你看不到textField上的输入。 I need to only use the pickerView on top textField so the view won't move up when that textfield is being used. 我只需要在顶部textField上使用pickerView,这样在使用该文本字段时视图就不会向上移动。 Here is what i'm using to move view up: 以下是我用来移动视图的内容:

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

Here is my setup for the pickerView in viewDidLoad: (colorCodePicker is the textField with pickerView as input) 这是我在viewDidLoad中的pickerView的设置:(colorCodePicker是带有pickerView作为输入的textField)

let pickerView = UIPickerView()

    pickerView.delegate = self

    pickerView.backgroundColor = UIColor.whiteColor()

    colorCodePicker.inputView = pickerView

I have tried most of the solutions here but haven't got any to work ie if i disable user input than i can't call the pickerView, also tried inserting a clear button over the textField like one of the suggestions with no luck. 我已经尝试了大多数解决方案,但没有任何工作,即如果我禁用用户输入,我不能调用pickerView,也尝试在textField上插入一个清除按钮,像一个没有运气的建议。 Or maybe a solution to only move view up when bottom textField is active, any ideas? 或者也许只是在底部textField处于活动状态时才移动视图的解决方案,任何想法?

I know it won't be exact answer but if you just use IQKeyboardManager you do not need to think about those staff in any scene of your application. 我知道这不是确切的答案,但如果您只是使用IQKeyboardManager,则无需在应用程序的任何场景中考虑这些人员。

IQKeyboardManager IQKeyboardManager

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

There can be quick fixes to save the day but I'd suggest using UITableViewController for building form pages. 可以快速修复以节省一天,但我建议使用UITableViewController来构建表单页面。 It automatically does keyboard handling. 它会自动进行键盘处理。 Yes, you need to create UITableViewCell s which contain UITextField s but I think this is feasible. 是的,您需要创建包含UITextFieldUITableViewCell ,但我认为这是可行的。

I do not like using stand-alone UITableViewController since they do not have a UIView as their self.view , therefore I embed them to my view controller and access its tableView during segue. 我不喜欢使用独立的UITableViewController因为他们没有UIView作为他们的self.view ,因此我将它们嵌入到我的视图控制器并在segue期间访问它的tableView Even if it is not dataSource / delegate of its tableView , it handles keyboard. 即使它不是tableView dataSource / delegate ,它也会处理键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM