您好,登錄后才能下訂單哦!
小編給大家分享一下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,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].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, 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 - 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
看完了這篇文章,相信你對“iOS鍵盤怎么添加隱藏鍵盤功能”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。