介绍
小编给大家分享一下iOS键盘怎么添加隐藏键盘功能,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!
<强>键盘添加个隐藏键盘功能强>
<强>使用方法:>强导入XMCustomKeyBoard。h
[XMCustomKeyBoard CancelableKeyboard:控件对象],
控件对象可以是UITextFiled, UITextView, UISearchBar等一系列调用键盘输入的类的实例
1。自定义个UIBarButtonItem,添加属性editableView, editableView存储需要添加隐藏键盘功能的那个控件
# import & lt; UIKit/UIKit.h> @interface XMCustomKeyBoardBtn : UIBarButtonItem @property (强,,原子),id editableView; @end
# import “XMCustomKeyBoardBtn.h" @implementation XMCustomKeyBoardBtn @end
2。自定义个UIView,因为只有UIView的子类才可以添加进keyWindow,想动态绑定这个类定义的方法,就必须让这个类保持活跃。
# import & lt;基?Foundation.h> # import & lt; UIKit/UIKit.h> # import “XMCustomKeyBoardBtn.h" @interface XMCustomKeyBoard : UIView +,(空白),CancelableKeyboard: (id), editableView; +,(空白),CancelableKeyboard: (id), editableView CustomButtonItem: btn (UIBarButtonItem *); @end
3。通过传进来的控件为其在键盘工具栏添加一个隐藏键盘的按钮并动态绑定一个隐藏键盘的方法
# import “XMCustomKeyBoard.h" @implementation XMCustomKeyBoard +,(空白),CancelableKeyboard: (id), editableView { XMCustomKeyBoard 才能;* custom =, [[XMCustomKeyBoard alloc], initWithFrame: CGRectMake (0、-999、10、10)]; ,,[[UIApplication sharedApplication] .keyWindow addSubview:自定义); ,,[editableView setInputAccessoryView: [self CancelableKeyboardToolBar: editableView addTarget:自定义]]; } +,(空白),CancelableKeyboard: (id), editableView CustomButtonItem: (UIBarButtonItem *) btn { XMCustomKeyBoard 才能;* custom =, [[XMCustomKeyBoard alloc], initWithFrame: CGRectMake (0、-10、10、10)]; ,,[[UIApplication sharedApplication] .keyWindow addSubview:自定义); ,,[editableView setInputAccessoryView: [self CancelableKeyboardToolBar: editableView CustomButtonItem: btn addTarget:自定义]]; } +,(UIToolbar *) CancelableKeyboardToolBar: (id), editableView CustomButtonItem: (UIBarButtonItem *) btn addTarget: (id),目标 { UIToolbar 才能;* toolbar =, [[UIToolbar alloc], initWithFrame: CGRectMake (0, 0, CGRectGetWidth ([UIApplication sharedApplication] .keyWindow.frame), 40)]; 时间=toolbar.backgroundColor 才能;[UIColor lightGrayColor]; ,, UIBarButtonItem 才能;* button =, [[UIBarButtonItem alloc], initWithTitle: @",,,风格:UIBarButtonItemStylePlain 目标:editableView 行动:@ selector (onClick)]; [才能button setWidth: [UIApplication sharedApplication] .keyWindow.frame.size.width 作用;btn.width); XMCustomKeyBoardBtn 才能;* button1 =, (XMCustomKeyBoardBtn *) btn; ,, 时间=button1.target 才能;目标; ,, button1.action 才能=,@ selector (CancelableKeyboard:); ,, 时间=button1.editableView 才能;editableView; ,,,,,,,, (才能toolbar setItems: @[按钮,button1]]; return 才能,工具栏; } +,(UIToolbar *) CancelableKeyboardToolBar: (id), editableView addTarget: (id),目标 { UIToolbar 才能;* toolbar =, [[UIToolbar alloc], initWithFrame: CGRectMake (0, 0, CGRectGetWidth ([UIApplication sharedApplication] .keyWindow.frame), 40)]; 时间=toolbar.backgroundColor 才能;[UIColor lightGrayColor]; ,, UIBarButtonItem 才能;* button =, [[UIBarButtonItem alloc], initWithTitle: @",,,风格:UIBarButtonItemStylePlain 目标:editableView 行动:@ selector (onClick)]; [才能button setWidth: [UIApplication sharedApplication] .keyWindow.frame.size.width 作用;50); XMCustomKeyBoardBtn 才能;* button1 =, [[XMCustomKeyBoardBtn alloc], initWithTitle: @"隐藏键盘“,风格:UIBarButtonItemStyleBordered 目标:target 行动:@ selector (CancelableKeyboard:)]; ,, 时间=button1.editableView 才能;editableView; ,,,,,,,, ,,(button1 setWidth: 50); (才能toolbar setItems: @[按钮,button1]]; return 才能,工具栏; } - (void) CancelableKeyboard:(XMCustomKeyBoardBtn *), btn { (才能btn.editableView resignFirstResponder); } (空白),onClick { ,, } @endiOS键盘怎么添加隐藏键盘功能