1. 程式人生 > 程式設計 >iOS鍵盤如何新增隱藏鍵盤功能

iOS鍵盤如何新增隱藏鍵盤功能

本文例項為大家分享了iOS新增隱藏鍵盤功能的具體方法,供大家參考,具體內容如下

鍵盤添加個隱藏鍵盤功能

使用方法:匯入XMCustomKeyBoard.h
[XMCustomKeyBoard CancelableKeyboard:控制元件物件 ];
控制元件物件可以是UITextFiled,UITextView,UISearchBar 等一系列呼叫鍵盤輸入的類的例項

1.自定義個UIBarButtonItem,新增屬性editableView,editableView儲存需要新增隱藏鍵盤功能的那個控制元件

#import <UIKit/UIKit.h>

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

@end
#import "XMCustomKeyBoardBtn.h"

@implementation XMCustomKeyBoardBtn


@end

2.自定義個UIView,因為只有UIView的子類才可以新增進keyWindow,想動態繫結這個類定義的方法,就必須讓這個類保持活躍。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "XMCustomKeyBoardBtn.h"


@interface XMCustomKeyBoard : UIView

+ (void) CancelableKeyboard:(id) editableView;

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

@end

3.通過傳進來的控制元件為其在鍵盤工具欄新增一個隱藏鍵盤的按鈕並動態繫結一個隱藏鍵盤的方法

#import "XMCustomKeyBoard.h"

@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)];
  [[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,CGRectGetWidth([UIApplication sharedApplication].keyWindow.frame),40)];
  toolbar.backgroundColor = [UIColor lightGrayColor];
  
  UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:editableView action:@selector(onClick)];
  [button setWidth:[UIApplication sharedApplication].keyWindow.frame.size.width - btn.width];

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

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

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

@end

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。/p>