iOS鍵盤如何添加隱藏鍵盤功能
本文實例為大家分享了iOS添加隱藏鍵盤功能的具體方法,供大家參考,具體內(nèi)容如下
鍵盤添加個隱藏鍵盤功能
使用方法:導(dǎo)入XMCustomKeyBoard.h
[XMCustomKeyBoard CancelableKeyboard:控件對象 ];
控件對象可以是UITextFiled,UITextView,UISearchBar 等一系列調(diào)用鍵盤輸入的類的實例
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,想動態(tà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.通過傳進來的控件為其在鍵盤工具欄添加一個隱藏鍵盤的按鈕并動態(tài)綁定一個隱藏鍵盤的方法
#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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。/p>
相關(guān)文章
解析Objective-C?中?`+load`?方法的執(zhí)行順序
在?Objective-C?中,+load?方法是在類或分類被加載到內(nèi)存時調(diào)用的,它在程序啟動過程中非常早的階段執(zhí)行,用于在類或分類被加載時進行一些初始化工作,這篇文章主要介紹了?Objective-C?中?`+load`?方法的執(zhí)行順序,需要的朋友可以參考下2024-07-07
iOS中使用UIDatePicker制作時間選擇器的實例教程
這篇文章主要介紹了iOS中使用UIDatePicker制作時間選擇器的實例教程,實例中未選中的時間項目會講解一個將其變透明的方法,非常給力,需要的朋友可以參考下2016-05-05
iOS應(yīng)用中UITableView左滑自定義選項及批量刪除的實現(xiàn)
這篇文章主要介紹了iOS應(yīng)用中UITableView左滑自定義選項及批量刪除的實現(xiàn),UITableView列表中即通訊錄左滑呼出選項的那種效果在刪除時能夠?qū)崿F(xiàn)多行刪除將更加方便,需要的朋友可以參考下2016-03-03
iOS開發(fā)使用XML解析網(wǎng)絡(luò)數(shù)據(jù)
XML解析其實這個概念出現(xiàn)了算夠久了,以前javaweb什么到處都在用。這邊我們主要大致介紹下,然后在在ios編程如何使用。2016-02-02

