小編給大家分享一下iOS鍵盤怎么添加隱藏鍵盤功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),咸寧企業(yè)網(wǎng)站建設(shè),咸寧品牌網(wǎng)站建設(shè),網(wǎng)站定制,咸寧網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,咸寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
鍵盤添加個(gè)隱藏鍵盤功能
使用方法:導(dǎo)入XMCustomKeyBoard.h
[XMCustomKeyBoard CancelableKeyboard:控件對(duì)象 ];
控件對(duì)象可以是UITextFiled,UITextView,UISearchBar 等一系列調(diào)用鍵盤輸入的類的實(shí)例
1.自定義個(gè)UIBarButtonItem,添加屬性editableView,editableView存儲(chǔ)需要添加隱藏鍵盤功能的那個(gè)控件
#import@interface XMCustomKeyBoardBtn : UIBarButtonItem @property (strong, nonatomic) id editableView; @end
#import "XMCustomKeyBoardBtn.h" @implementation XMCustomKeyBoardBtn @end
2.自定義個(gè)UIView,因?yàn)橹挥蠻IView的子類才可以添加進(jìn)keyWindow,想動(dòng)態(tài)綁定這個(gè)類定義的方法,就必須讓這個(gè)類保持活躍。
#import#import #import "XMCustomKeyBoardBtn.h" @interface XMCustomKeyBoard : UIView + (void) CancelableKeyboard:(id) editableView; + (void) CancelableKeyboard:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn; @end
3.通過傳進(jìn)來的控件為其在鍵盤工具欄添加一個(gè)隱藏鍵盤的按鈕并動(dòng)態(tài)綁定一個(gè)隱藏鍵盤的方法
#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
看完了這篇文章,相信你對(duì)“iOS鍵盤怎么添加隱藏鍵盤功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!