iOS键盘如何添加隐藏键盘功能

所属分类: 软件编程 / IOS 阅读数: 1676
收藏 0 赞 0 分享

本文实例为大家分享了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画出精美的图表方法示例

这篇文章主要给大家介绍了关于iOS如何画出精美的图表的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

ios 服务器端推送证书生成的方法

这篇文章主要介绍了ios 服务器端推送证书生成的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈iOS推送证书生成pem文件(详细生成过程)

这篇文章主要介绍了浅谈iOS推送证书生成pem文件(详细生成过程),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS高仿微信文章悬浮球功能

这篇文章主要介绍了iOS高仿微信文章悬浮球功能,本文给大家介绍的非常详细,具有一定的参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

通过一行代码搞定UITextField的输入格式限制

这篇文章主要给大家介绍了如何通过一行代码搞定UITextField的输入格式限制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS判断是否越狱设备方法示例

这篇文章主要给大家介绍了关于iOS判断是否越狱设备的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS开发教程之Status Bar状态栏设置的方法汇总

iOS 的 Status Bar 状态栏是一个比较坑的地方,所以下面这篇文章主要给大家介绍了关于iOS开发教程之Status Bar状态栏设置的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发之导航栏各种右滑返回失效的解决方法汇总

这篇文章主要给大家总结介绍了关于iOS开发教程之导航栏各种右滑返回失效的解决方法,文中通过示例代码介绍的非常详细,对各位iOS具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS实现简单的头部缩放功能

这篇文章主要介绍了iOS 简单的头部缩放效果,头部伴随模糊效果放大缩小,并在一定位置时悬停充当导航栏,本文给大家提供实现思路,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS中震动反馈(UIFeedbackGenerator)与系统震动详解

最近要做一个项目,需要持续响铃并振动,所以就有了这篇文章,下面这篇文章主要给大家介绍了关于iOS中震动反馈(UIFeedbackGenerator)与系统震动的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多