欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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>

相关标签: iOS 键盘