iOS键盘如何添加隐藏键盘功能
本文实例为大家分享了iOS添加隐藏键盘功能的具体方法,供大家参考,具体内容如下
键盘添加个隐藏键盘功能
使用方法:导入XMCustomKeyBoard.h
[XMCustomKeyBoardCancelableKeyboard:控件对象];
控件对象可以是UITextFiled,UITextView,UISearchBar等一系列调用键盘输入的类的实例
1.自定义个UIBarButtonItem,添加属性editableView,editableView存储需要添加隐藏键盘功能的那个控件
#import@interfaceXMCustomKeyBoardBtn:UIBarButtonItem @property(strong,nonatomic)ideditableView; @end
#import"XMCustomKeyBoardBtn.h" @implementationXMCustomKeyBoardBtn @end
2.自定义个UIView,因为只有UIView的子类才可以添加进keyWindow,想动态绑定这个类定义的方法,就必须让这个类保持活跃。
#import#import #import"XMCustomKeyBoardBtn.h" @interfaceXMCustomKeyBoard:UIView +(void)CancelableKeyboard:(id)editableView; +(void)CancelableKeyboard:(id)editableViewCustomButtonItem:(UIBarButtonItem*)btn; @end
3.通过传进来的控件为其在键盘工具栏添加一个隐藏键盘的按钮并动态绑定一个隐藏键盘的方法
#import"XMCustomKeyBoard.h" @implementationXMCustomKeyBoard +(void)CancelableKeyboard:(id)editableView{ XMCustomKeyBoard*custom=[[XMCustomKeyBoardalloc]initWithFrame:CGRectMake(0,-999,10,10)]; [[UIApplicationsharedApplication].keyWindowaddSubview:custom]; [editableViewsetInputAccessoryView:[selfCancelableKeyboardToolBar:editableViewaddTarget:custom]]; } +(void)CancelableKeyboard:(id)editableViewCustomButtonItem:(UIBarButtonItem*)btn{ XMCustomKeyBoard*custom=[[XMCustomKeyBoardalloc]initWithFrame:CGRectMake(0,-10,10,10)]; [[UIApplicationsharedApplication].keyWindowaddSubview:custom]; [editableViewsetInputAccessoryView:[selfCancelableKeyboardToolBar:editableViewCustomButtonItem:btnaddTarget:custom]]; } +(UIToolbar*)CancelableKeyboardToolBar:(id)editableViewCustomButtonItem:(UIBarButtonItem*)btnaddTarget:(id)target { UIToolbar*toolbar=[[UIToolbaralloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth([UIApplicationsharedApplication].keyWindow.frame),40)]; toolbar.backgroundColor=[UIColorlightGrayColor]; UIBarButtonItem*button=[[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:editableViewaction:@selector(onClick)]; [buttonsetWidth:[UIApplicationsharedApplication].keyWindow.frame.size.width-btn.width]; XMCustomKeyBoardBtn*button1=(XMCustomKeyBoardBtn*)btn; button1.target=target; button1.action=@selector(CancelableKeyboard:); button1.editableView=editableView; [toolbarsetItems:@[button,button1]]; returntoolbar; } +(UIToolbar*)CancelableKeyboardToolBar:(id)editableViewaddTarget:(id)target { UIToolbar*toolbar=[[UIToolbaralloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth([UIApplicationsharedApplication].keyWindow.frame),40)]; toolbar.backgroundColor=[UIColorlightGrayColor]; UIBarButtonItem*button=[[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:editableViewaction:@selector(onClick)]; [buttonsetWidth:[UIApplicationsharedApplication].keyWindow.frame.size.width-50]; XMCustomKeyBoardBtn*button1=[[XMCustomKeyBoardBtnalloc]initWithTitle:@"隐藏键盘"style:UIBarButtonItemStyleBorderedtarget:targetaction:@selector(CancelableKeyboard:)]; button1.editableView=editableView; [button1setWidth:50]; [toolbarsetItems:@[button,button1]]; returntoolbar; } -(void)CancelableKeyboard:(XMCustomKeyBoardBtn*)btn{ [btn.editableViewresignFirstResponder]; } -(void)onClick{ } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。/p>
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。