iOS键盘如何添加隐藏键盘功能
程序员文章站
2023-02-22 15:53:11
本文实例为大家分享了ios添加隐藏键盘功能的具体方法,供大家参考,具体内容如下键盘添加个隐藏键盘功能使用方法:导入xmcustomkeyboard.h[xmcustomkeyboard cancela...
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。/p>
推荐阅读
-
jQuery实现手机上输入后隐藏键盘功能
-
Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)
-
Android编程之ListView和EditText发布帖子隐藏软键盘功能详解
-
Android编程实现点击EditText之外的控件隐藏软键盘功能
-
html移动应用 input 标签 清除按钮功能如何实现(不触发键盘)
-
电脑如何添加外语键盘?
-
iOS开发- runtime基本用法解析和用runtime给键盘添加工具栏和按钮响应事件
-
iOS键盘如何添加隐藏键盘功能
-
IOS 隐藏软键盘的3种方法
-
**如何解决ios点击输入框后键盘弹起,键盘收起后,输入框不恢复原位置,页面不能回弹导致下方有空白,弹框的确定按钮不能点击**