iOS鍵盤(pán)如何添加隱藏鍵盤(pán)功能
本文實(shí)例為大家分享了iOS添加隱藏鍵盤(pán)功能的具體方法,供大家參考,具體內(nèi)容如下
鍵盤(pán)添加個(gè)隱藏鍵盤(pán)功能
使用方法:導(dǎo)入XMCustomKeyBoard.h
[XMCustomKeyBoard CancelableKeyboard:控件對(duì)象 ];
控件對(duì)象可以是UITextFiled,UITextView,UISearchBar 等一系列調(diào)用鍵盤(pán)輸入的類(lèi)的實(shí)例
1.自定義個(gè)UIBarButtonItem,添加屬性editableView,editableView存儲(chǔ)需要添加隱藏鍵盤(pán)功能的那個(gè)控件
#import <UIKit/UIKit.h> @interface XMCustomKeyBoardBtn : UIBarButtonItem @property (strong, nonatomic) id editableView; @end
#import "XMCustomKeyBoardBtn.h" @implementation XMCustomKeyBoardBtn @end
2.自定義個(gè)UIView,因?yàn)橹挥蠻IView的子類(lèi)才可以添加進(jìn)keyWindow,想動(dòng)態(tài)綁定這個(gè)類(lèi)定義的方法,就必須讓這個(gè)類(lèi)保持活躍。
#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.通過(guò)傳進(jìn)來(lái)的控件為其在鍵盤(pán)工具欄添加一個(gè)隱藏鍵盤(pán)的按鈕并動(dòng)態(tài)綁定一個(gè)隱藏鍵盤(pán)的方法
#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:@"隱藏鍵盤(pán)" 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。/p>
- 詳解IOS點(diǎn)擊空白處隱藏鍵盤(pán)的幾種方法介紹
- 總結(jié)IOS中隱藏軟鍵盤(pán)的三種方式
- 解決ios模擬器不能彈出鍵盤(pán)問(wèn)題的方法
- iOS應(yīng)用開(kāi)發(fā)中監(jiān)聽(tīng)鍵盤(pán)事件的代碼實(shí)例小結(jié)
- 總結(jié)IOS關(guān)閉鍵盤(pán)/退出鍵盤(pán)的五種方式
- 解決vue js IOS H5focus無(wú)法自動(dòng)彈出鍵盤(pán)的問(wèn)題
- IOS關(guān)閉鍵盤(pán)的方法
- iOS讓軟鍵盤(pán)消失的簡(jiǎn)單方法
- 代碼詳解ios鍵盤(pán)收起問(wèn)題
- iOS 開(kāi)發(fā)之 - 關(guān)閉鍵盤(pán) 退出鍵盤(pán) 的5種方式
相關(guān)文章
詳解iOS應(yīng)用使用Storyboard布局時(shí)的IBOutlet與IBAction
這篇文章主要介紹了iOS應(yīng)用使用Storyboard布局時(shí)的IBOutlet與IBAction,文中還附帶講解了為什么IBOutlet屬性是weak的,需要的朋友可以參考下2016-04-04iOS開(kāi)發(fā)創(chuàng)建frame實(shí)現(xiàn)window窗口view視圖示例
這篇文章主要為大家介紹了iOS開(kāi)發(fā)創(chuàng)建frame實(shí)現(xiàn)window窗口view視圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度
這篇文章主要為大家詳細(xì)介紹了iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12iOS開(kāi)發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤(pán)自定義按鍵
這篇文章主要介紹了iOS開(kāi)發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤(pán)自定義按鍵 的相關(guān)資料,需要的朋友可以參考下2016-08-08iOS Touch ID指紋識(shí)別技術(shù)簡(jiǎn)介
這篇文章主要為大家詳細(xì)介紹了iOS Touch ID指紋識(shí)別技術(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04