How to disable keyboard appearing when hitting on a text field , iOS?

后端 未结 8 1398
日久生厌
日久生厌 2021-02-07 19:50

I have a text field , and i need when the user presses it to show a custom picker.

The picker is shown fine , but the problem is that the keyboard appears on the bottom

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 20:27

    It looks like all of these answers take one approach, to simply deny the keyboard before it comes up. This prevents first responder status, which has many advantages.

    One simple approach that allows you to maintain first responder status is to create an empty view and assign that to the inputView property on your input field. If you are using iOS 9 (or later?) you will also have to get rid of the inputAssistantItem objects as well.

    UITextField *field = [[UITextField alloc] init];
    field.inputView = self.emptyKeyboard.view;
    
    UITextInputAssistantItem *aItem = [field inputAssistantItem];
    aItem.leadingBarButtonGroups = @[];
    aItem.trailingBarButtonGroups = @[];
    

    Then if you want to control the field from an alternate view controller, you can do so by adding targets:

    [field addTarget:self.numberPad action:@selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
    [field addTarget:self.numberPad action:@selector(editingEnded:) forControlEvents:UIControlEventEditingDidEnd];
    [field addTarget:self.numberPad action:@selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];
    

    It is also possible to do this a lot more cleanly by subclassing UITextField.

提交回复
热议问题