SoFunction
Updated on 2025-04-03

How to add hidden keyboard feature for iOS keyboard

This article shares with you the specific method of adding hidden keyboard function on iOS for your reference. The specific content is as follows

Add a hidden keyboard function on the keyboard

How to use:Import
[XMCustomKeyBoard CancelableKeyboard:Control Object];
The control object can be a series of instances of classes such as UITextFiled, UITextView, UISearchBar, etc.

1. Customize a UIBarButtonItem, add the attribute editableView, and editableView stores the control that needs to be added to hide the keyboard function.

#import <UIKit/>

@interface XMCustomKeyBoardBtn : UIBarButtonItem
@property (strong, nonatomic) id editableView;

@end
#import ""

@implementation XMCustomKeyBoardBtn


@end

2. Customize a UIView, because only the subclass of the UIView can be added to the keyWindow. If you want to dynamically bind the method defined by this class, you must keep the class active.

#import <Foundation/>
#import <UIKit/>
#import ""


@interface XMCustomKeyBoard : UIView

+ (void) CancelableKeyboard:(id) editableView;

+ (void) CancelableKeyboard:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn;

@end

3. Add a hidden keyboard button to the keyboard toolbar through the passed in control and dynamically bind a hidden keyboard.

#import ""

@implementation XMCustomKeyBoard

+ (void) CancelableKeyboard:(id) editableView{
  XMCustomKeyBoard *custom = [[XMCustomKeyBoard alloc] initWithFrame:CGRectMake(0,-999,10,10)];
  [[UIApplication sharedApplication].keyWindow addSubview:custom];
  [editableView setInputAccessoryView:[self CancelableKeyboardToolBar:editableView addTarget:custom]];
}

+ (void) CancelableKeyboard:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn {
  XMCustomKeyBoard *custom = [[XMCustomKeyBoard alloc] initWithFrame:CGRectMake(0,-10,10,10)];
  [[UIApplication sharedApplication].keyWindow addSubview:custom];
  [editableView setInputAccessoryView:[self CancelableKeyboardToolBar:editableView CustomButtonItem:btn addTarget:custom]];
}

+ (UIToolbar *)CancelableKeyboardToolBar:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn addTarget:(id) target
{
  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIApplication sharedApplication].), 40)];
   = [UIColor lightGrayColor];
  
  UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:editableView action:@selector(onClick)];
  [button setWidth:[UIApplication sharedApplication]. - ];

  XMCustomKeyBoardBtn *button1 = (XMCustomKeyBoardBtn *)btn;
  
   = target;
  
   = @selector(CancelableKeyboard:);
  
   = editableView;
         
  [toolbar setItems:@[button,button1]];
  return toolbar;
}

+ (UIToolbar *)CancelableKeyboardToolBar:(id) editableView addTarget:(id) target
{
  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIApplication sharedApplication].), 40)];
   = [UIColor lightGrayColor];
  
  UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:editableView action:@selector(onClick)];
  [button setWidth:[UIApplication sharedApplication]. - 50];

  XMCustomKeyBoardBtn *button1 = [[XMCustomKeyBoardBtn alloc] initWithTitle:@"Hide keyboard" style:UIBarButtonItemStyleBordered target:target action:@selector(CancelableKeyboard:)];
  
   = editableView;
         
  [button1 setWidth:50];
  [toolbar setItems:@[button,button1]];
  return toolbar;
}
-(void)CancelableKeyboard:(XMCustomKeyBoardBtn *) btn{
  [ resignFirstResponder];
}
-(void) onClick{
  
}

@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more. /p>