ios键盘通知和自定义键盘

一.键盘通知

当文本View(如UITextField,UITextView, UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘。成为firstresponder可能由用户点击触发,也可向文本View发送becomeFirstResponder消息触发。当文本视图退出first responder时,键盘会消失。文本View退出first responder可能由用户点击键盘上的Done或Return键结束输入触发,也可向文本View发送resignFirstResponder消息触发。

当键盘显示或消失时,系统会发送相关的通知:
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification

通知消息 NSNotification中的 userInfo字典中包含键盘的位置和大小信息,对应的key为

UIKeyboardFrameBeginUserInfoKey
UIKeyboardFrameEndUserInfoKey
UIKeyboardAnimationDurationUserInfoKey
UIKeyboardAnimationCurveUserInfoKey

UIKeyboardFrameBeginUserInfoKey,UIKeyboardFrameEndUserInfoKey对应的Value是个NSValue对象,内部包含CGRect结构,分别为键盘起始时和终止时的位置信息。

UIKeyboardAnimationCurveUserInfoKey对应的Value是NSNumber对象,内部为UIViewAnimationCurve类型的数据,表示键盘显示或消失的动画类型。

UIKeyboardAnimationDurationUserInfoKey对应的Value也是NSNumber对象,内部为double类型的数据,表示键盘h显示或消失时动画的持续时间。

例如,在UIKeyboardWillShowNotification,UIKeyboardDidShowNotification通知中的userInfo内容为

userInfo = {UIKeyboardAnimationCurveUserInfoKey = 0;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";UIKeyboardFrameChangedByUserInteraction = 0;UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
}

在UIKeyboardWillHideNotification,UIKeyboardDidHideNotification通知中的
userInfo内容为:

userInfo = {UIKeyboardAnimationCurveUserInfoKey = 0;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";UIKeyboardFrameChangedByUserInteraction = 0;UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
}

在Text,Web, and Editing Programming Guide for iOS中,有如下描述Note: The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always {0.0, 0.0}) in rectangle-intersection operations. Because the keyboard is animated into position, the actual bounding rectangle of the keyboard changes over time.
但从实际获取的信息来看,矩形的origin并不为{0.0, 0.0},这里应该不准确。

二.文本对象与WebView键盘设置

UITextFiled和 UITextView都遵循 UITextInputTraits协议,在UITextInputTraits协议中定义了设置键盘的属性,有

1. keyboardType:键盘类型,如UIKeyboardTypeDefault,UIKeyboardTypeURL,UIKeyboardTypeEmailAddress,UIKeyboardTypePhonePad等

2. returnKeyType:键盘Return键显示的文本,默认为”Return”,其他可选择的包括Go,Next,Done,Send,Google等。

3. keyboardAppearance:键盘外观,默认为 UIKeyboardAppearanceDefault,即上图中的浅兰色不透明背景,另外还有一种为 UIKeyboardAppearanceAlert,
键盘背景为黑色半透明,用于在警告框输入时显示,例如appStore中输入密码时:


若想显示黑色键盘又不想透明露出底部视图,可以将键盘配置成Alert类型的,然后监听键盘显示的广播通知,在显示键盘时在键盘底部增加一不透明黑色背景视图。

注:在苹果的键盘示例程序 KeyboardAccessory中,将UITextView键盘类型更改为UIKeyboardAppearanceAlert,在iPad模拟器中运行键盘并没有出现黑色透明的效果,不知为何? 在iPhone中UIKeyboardAppearanceAlert设置有效。

4.autocapitalizationType:文本大小写样式,见 UITextAutocapitalizationType。
5.autocorrectionType:是否自动更正,见 UITextAutocorrectionType。
6.spellCheckingType:拼写检查设置,见UITextSpellCheckingType。
7. enablesReturnKeyAutomatically:是否在无文本时禁用Return键,默认为NO。若为YES,则用户至少输入一个字符后Return键才被激活。 
8. secureTextEntry:若输入的是密码,可设置此类型为YES,输入字符时可显示最后一个字符,其他字符显示为点。

UIWebView本身不直接遵循 UITextInputTraits协议,但同样可设置其内部输入部件的键盘属性。如Configuring the Keyboard for Web Views中所述。

设置autocorrect, auto-capitalization属性。

<input type="text" size="30" autocorrect="off" autocapitalization="on">

设置键盘类型:

Text: <input type="text"></input>Telephone: <input type="tel"></input>URL: <input type="url"></input>Email: <input type="email"></input>Zip code: <input type="text" pattern="[0-9]*"></input>

三. 使用键盘通知调整文本视图位置

当文本视图成为First Responser时在窗口底部会显示出键盘,显示的键盘很可能会将文本视图盖住从而无法看到编辑的效果。键盘通知的一大用途即在键盘显示或隐藏时获取到键盘的位置信息,从而可以调整窗口中的文本视图位置或大小,使其可以在键盘上方显示。

Text, Web, and Editing Programming Guide for iOS中的Moving
Content That Is Located Under the Keyboard节在键盘显示和消失通知中,通过调整内容UIScrollView视图的 contentInset和 contentOffset来保证编辑的文本视图不会被键盘盖住。

其流程为
1.在初始化( viewDidLoad或viewWillAppear)时,注册处理键盘通知的方法。

- (void)registerForKeyboardNotifications
{[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWasShown:)name:UIKeyboardDidShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillBeHidden:)name:UIKeyboardWillHideNotification object:nil];}

2.在键盘显示的通知事件处理中获取到即将显示键盘的大小,将 UIScrollView的contentInset设置为键盘的frame区域,同样设置scrollIndicatorInsets保证滚动条不会被键盘盖住。获取到编辑文本视图的原点位置,与键盘显示区域比较,若会被键盘覆盖,则调整 contentOffset以使其在键盘上方

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{NSDictionary* info = [aNotification userInfo];CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);scrollView.contentInset = contentInsets;scrollView.scrollIndicatorInsets = contentInsets;// If active text field is hidden by keyboard, scroll it so it's visible// Your application might not need or want this behavior.CGRect aRect = self.view.frame;aRect.size.height -= kbSize.height;if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);[scrollView setContentOffset:scrollPoint animated:YES];}
}

3.在键盘消失的通知处理事件中,简单的将UIScrollView恢复即可

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{UIEdgeInsets contentInsets = UIEdgeInsetsZero;scrollView.contentInset = contentInsets;scrollView.scrollIndicatorInsets = contentInsets;
}

在苹果的 KeyboardAccessory示例程序中同样演示了使用键盘通知来调整文本视图位置的代码,其中使用了键盘通知中的键盘动画时间,从而使文本视图移动的动画与键盘的显示和消失同步。

- (void)keyboardWillShow:(NSNotification *)notification {/*Reduce the size of the text view so that it's not obscured by the keyboard.Animate the resize so that it's in sync with the appearance of the keyboard.*/NSDictionary *userInfo = [notification userInfo];// Get the origin of the keyboard when it's displayed.NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.CGRect keyboardRect = [aValue CGRectValue];keyboardRect = [self.view convertRect:keyboardRect fromView:nil];CGFloat keyboardTop = keyboardRect.origin.y;CGRect newTextViewFrame = self.view.bounds;newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;// Get the duration of the animation.NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];NSTimeInterval animationDuration;[animationDurationValue getValue:&animationDuration];// Animate the resize of the text view's frame in sync with the keyboard's appearance.[UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:animationDuration];textView.frame = newTextViewFrame;[UIView commitAnimations];
}- (void)keyboardWillHide:(NSNotification *)notification {NSDictionary* userInfo = [notification userInfo];/*Restore the size of the text view (fill self's view).Animate the resize so that it's in sync with the disappearance of the keyboard.*/NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];NSTimeInterval animationDuration;[animationDurationValue getValue:&animationDuration];[UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:animationDuration];textView.frame = self.view.bounds;[UIView commitAnimations];
}

关于调整文本视图大小的代码,还可以参考Different way to show keyboard and resize UIView with text field。

还可以在键盘通知中实现自定义键盘,例如UIKEYBOARDTYPENUMBERPAD AND THE MISSING “RETURN” KEY中使用键盘通知在数字键盘中增加Return键,还可参考UIKeyboardTypeNumberPad键盘增加Return键

注:若在同一窗口中有两个UITextField,当第一个UITextField成为First Responser时,开始显示键盘并接收到UIKeyboardWillShowNotification,UIKeyboardDidShowNotification通知,当First Responser转移到第二个UITextField时,键盘会一直显示,此时不会收到WillShow和DidShow的通知。

四.使用inputAccessoryView与inputView定制输入视图

inputAccessoryView和 inputView属性在 UIResponder中定义,为readonly的属性,但在UITextFiled和 UITextView中重新定义为了readwrite的属性,可以由用户赋值。若 inputView的不为nil,则当文本视图成为first responder时,不会显示系统键盘,而是显示自定义的inputView;若inputAccessoryView不
为nil,则inputAccessoryView会显示在系统键盘或定制inputView的上方。当使用inputView时,仍然会有WillShow,DidShow,WillHide,DidHide的键盘通知,通知中的BeginFrame与EndFrame为系统键盘(或inputView)与inputAccessoryView一起的frame。

自定义inputAccessoryView非常常见,如编辑短信时的输入框


Web页面输入键盘


新浪微博评论界面

若想在输入时不使用系统键盘,而使用自定义的键盘,则可以设置inputView,如随手记中的金额输入时的键盘

若不想使用键盘输入,想从UIPickerView或UIDatePicker中选择,可设置inputView为对应的Picker视图,如图

苹果键盘示例程序 KeyboardAccessory演示了inputAccessoryView的使用方法。
 iOS,Want a “button” above a UIPicker or Keyboard, inputView,inputAccessoryView演示了在UIPickerView之上添加Toolbar的代码。

在标准视图中只有UITextField和UITextView将inputView和inputAccessoryView重新定义为了readwrite类型,若想在自定义视图中使用,需要在自定义视图中重新定义inputView和inputAccessoryView属性。见Input Views and Input Accessory Views。

参考:
Text, Web, and Editing Programming Guide for iOS – Managing the Keyboard
Text,Web, and Editing Programming Guide for iOS – Input Views and Input Accessory Views
UITextView Class Reference
UITextField Class Reference
UIResponder Class Reference
UIWindow Class Reference
UITextInputTraits Protocol Reference
KeyboardAccessory Demo
Different way to show keyboard and resize UIView with text field
iOS, Want a “button” above a UIPicker or Keyboard, inputView, inputAccessoryView
UIKeyboardFrameBeginUserInfoKey UIKeyboardFrameEndUserInfoKey
UIKeyboard键盘相关知识点-IOS开发
Change UISearchBar/Keyboard Search Button Title
iOS Human Interface Guidelines Keyboards and Input Views
How change keyboard background color in iOS?
Favorites UI design

本文出自 清风徐来,水波不兴 的博客.

本文永久链接: http://www.winddisk.com/2012/07/19/iphone_keyboard_notification_custom_keyboard/


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1383042.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

android实现仿真键盘(KeyboardView适配)

原生的键盘布局声明了deprecated被废弃&#xff0c;虽然可以使用但明显不合需求。 /*** deprecated This class is deprecated because this is just a convenient UI widget class that* application developers can re-implement on top of existing public API…

C#制作软键盘

本文下载地址&#xff1a;https://pan.baidu.com/s/1ubpeTwuQvAPNlC4QLDpp8w 提取码&#xff1a;1234 c#制作软键盘&#xff0c;先来个图。 上面这个图是制作的最终结果显示。 在制作键盘的时候&#xff0c;总体来说还是比较简单的&#xff0c;只有几个地方比较复杂一点。布…

C#自定义键盘

C#自定义键盘 一、创建自定义键盘DLL文件 1、新建一个类库 2、在类库项目中添加用户控件类 3、在用户控件上添加键盘需要的控件 4、添加如下代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Da…

IOS控件系列--优雅的表情键盘设计(扩展性好)

本控件效果如下图所示&#xff1a; 本控件设计的几个原则 &#xff1a; 1.扩展性好&#xff0c;代码不冗余。 2.逻辑清晰。 3.回调接口清晰。 4.移植性好。 首先来讨论下本控件的扩展性&#xff1a; 效果图给出的是3行7列。如果这个行列因子可控&#xff0c;起码可以应付…

坤音键盘(ikun专用)

坤音键位说明 &#xff08;下图第一个字母对应的是26键的键位&#xff0c;想听哪个按哪个。后面的数字代表时长&#xff0c;单位&#xff1a;秒&#xff09; 在右下角托盘里面可以右键关闭 ps&#xff1a;执行后桌面会出现一个ikun精灵&#xff0c;只要找一个能输入内容的地方…

Android自定义软键盘的实现

先看界面布局文件 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"fill_parent"android:layout_height"fill_parent"a…

Android自定义键盘(KeyboardView)

目录 1.场景&#xff1a;2.想法&#xff1a;3.开始实现&#xff1a;《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局&#xff1a;《二》创建IKeyboardView类并继承KeyboardView类,设置键盘布局&#xff08;数字和字母&#xff09;《三》 处理自定义键盘按键的点击事件…

关于技术转管理角色的认知

软件质量保障&#xff1a;所寫即所思&#xff5c;一个阿里质量人对测试的所感所悟。 程序员发展的岔路口 技术人做了几年专业工作之后&#xff0c;会来到一个重要的“分岔路口”&#xff0c;一边是专业的技术路线&#xff0c;一边是技术团队的管理路线。不少人就开始犯难&…

什么是响应式设计?列举几种实现响应式设计的方法。

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是响应式设计&#xff1f;⭐ 实现响应式设计的方法⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏…

【雕爷学编程】Arduino动手做(12)---霍尔模块之单极性霍尔开关器件AH3144E与线性霍尔传感器AH49E

37款传感器与模块的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&#x…

超级简单方法解决秒杀超卖和重复购买问题

一、基本原则 1、SQL加库存数量判断&#xff1a;防止库存变成负数 2.、数据库加唯一索引&#xff1a;防止用户重复购买 二、举例说明 1、开启事务 2、商品表减库存&#xff0c;库存数量要大于0 update miaosha_goods set stock_count stock_count - 1 where goods.id …

金雅拓超级狗superdog

超级狗是SafeNet在圣天诺基础上为中国本土量身定制的新一代加密狗。 超级狗操作简单&#xff0c;非常安全&#xff0c;功能实用&#xff0c;助您轻松完成高强度加密。 PDF和flv, swf, mpg, dat, avi, wav, mp3等加密&#xff09; 4、文档加密&#xff0c;ppt,word,excel,pdf&am…

赛孚耐SafeNet开发狗超级狗开发入门

本文主要讲解如果使用C#语言来对超级狗进行二次开发&#xff0c;如果仅仅是做个激活的功能&#xff0c;可以参照另一篇博客&#xff0c;地址&#xff1a;http://www.cnblogs.com/dathlin/p/8487842.html 如果疑问&#xff0c;可以加QQ群&#xff1a;592132877 继续主题研究&am…

在Elasticsearch中回测超级趋势线(Supertrend)交叉交易策略

我们已经讨论了好几个单一指标交易策略&#xff0c;其中简单的相对强弱指数&#xff08;RSI&#xff09;交易策略取得的利润最高。 在本文中&#xff0c;我们将使用 Elasticsearch 实现超级趋势线&#xff08;Supertrend&#xff09;交叉交易策略&#xff0c;并比较其性能是否优…

cmake基础(1)——简单项目

本节将围绕着hello world展开介绍如何创建一个简单的项目。 一、最小项目 1.准备工作 首先&#xff0c;新建一个目录01用来存放当前项目&#xff0c;并在目录下创建main.cpp和CMakeLists.txt文件&#xff0c;两者文件内容如下&#xff1a; #include <iostream> using…

Go把Map转成对象

最近使用了Redis的Hash&#xff0c;把一个对象给存储到了hash里面&#xff0c;具体如下&#xff1a; 现在需要从RedisHash缓存里面把结果给取出来&#xff0c;同时赋值到一个对象上面 result, err : global.GVA_REDIS.HGetAll(context.Background(), key).Result() 问题是resul…

优秀的LCD显示效果

优秀的LCD显示效果 效果图&#xff1a; 程序和源代码链接&#xff1a;http://download.csdn.net/download/zhangxiaoyu_sy/10012770

屏幕显示技术

本文主要介绍多种屏幕显示技术&#xff0c;主要是三大类&#xff0c;LCD&#xff0c;LED&#xff0c;OLED。 一、LCD LCD&#xff08;Liquid Crystal Display&#xff09;中文是液晶显示器。 LCD 的显示技术有很多&#xff0c;常见的有TFT&#xff0c;IPS&#xff0c;SLCD等…

物联网开发平台大 PK,谁是最佳 Pick?

通过这篇文章了解现下流行的几款物联网软件平台的各种功能。 作者 | Miyuru Dayarathna 译者 | 弯月&#xff0c;责编 | 郭芮 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09; 以下为译文&#xff1a; 本文以我们对各大物联网供应商的详细分析为基础&#xff0…

uni-app 2.2 发布,大幅度优化 H5 端性能体验 | 技术头条

作者 | uni-app团队 责编 | 伍杏玲 uni-app 自发布以来&#xff0c;已经服务了几十万开发者。但让我们意外&#xff0c;有大量开发者用uni-app只编写H5版&#xff0c;并没有多端发布。 这其实也符合uni-app的初衷&#xff0c;uni-app的定位并不是需要多端发布时才用uni-app。…