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:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@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。
ios之键盘的自定义的更多相关文章
- IOS开发-键盘通知自定义键盘
利用键盘通知可实现效果如下: 1.UIKeyboardWillShowNotification: 当点击到可输入的控件上(一般是UITextField)时,键盘会自动显示,并且触发UIKeyboard ...
- iOS 收起键盘的几种方式
iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...
- IOS 回收键盘通用代码
感觉IOS的键盘回收好累,所以封装了一个通用一点的方法 -(IBAction)spbResignFirstResponder:(id)sender { // NSLogObj(sender); if ...
- iOS开发多线程篇—自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- Windows 8 键盘上推自定义处理
原文:Windows 8 键盘上推自定义处理 在Windows 8 应用程序中,当TextBox控件获得焦点时,输入面板会弹出,如果TextBox控件处于页面下半部分,则系统会将页面上推是的TextB ...
- iOS学习——键盘弹出遮挡输入框问题解决方案
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...
- iOS 解决键盘挡住输入框的问题
iOS开发中经常会用到输入框UITextField,所以也常会遇到键盘挡住输入框而看不到输入框的内容. 在这里记录一种方法,用UITextField的代理来实现View的上移来解决这个问题. 首先设置 ...
- ios键盘弹起 body的高度拉长,页面底部空白问题。ios软键盘将页面抵到上面后,关闭软键盘页面不回弹的问题。
js 监听ios手机键盘弹起和收起的事件 /* js 监听ios手机键盘弹起和收起的事件 */ document.body.addEventListener('focusin', () => { ...
- ios处理键盘的大小
iOS的键盘有几个通知 UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotificat ...
随机推荐
- 生成Jar包 源码Jar包-字节码Jar包 不可运行Jar包-可运行Jar包
- D - Fliptile
#include <stdio.h> #include <iostream> #include <math.h> #include <algorithm> ...
- 使用JMETER进行REST API测试(分步指南)
我确定你在这里是因为你需要加载测试Json Rest API.这并不奇怪,因为Rest API现在越来越受欢迎. 这本指南的目的:帮助您进行负载测试一个Json的 REST API 通过一个具体的例子 ...
- laravel-admin 配置富文本编辑器流程
laravel-admin默认去除富文本编辑器的,官方也给出了配置方法. 我配置的是wangEditor,本来配置完后就能愉快得使用了,可万万没想到还是有坑的.默认是用base64上传的,也就是数据库 ...
- [软件工程基础]2017.10.27 第二次 Scrum 会议
决议 周六前项目交接 Milestone 完成 周六集体开发 游心整理物理网站上的实验流程和绪论复习题 石奇川上线静态版实验流程和绪论复习题库 李煦通构思后端如何实现绪论题库,包括和用户记录的关联方式 ...
- GYM 101673J(模拟)
本来我就模拟和搜索恐惧症,场上乍一看调度来调度去的真的吓得没敢写.然鹅赛后听说别的队写得贼短就写了写,真的不难--嘤嘤嘤 #include <cstdio> #include <cs ...
- 牛客练习赛42A(字符串)
传送门 结论是:一定是选取最长的那个AB连续子串. 把题面要求的a*b + a + b转化一下成(a + 1)*(b + 1) - 1,即可发现如果选取前缀后缀不连续的两段作为答案,则显然有更优解,即 ...
- 16 Groovy 和并发
Gpars是groovy的并行处理框架.其支持Actors, Map/Reduce, Dataflow, Fork/Join.关注更多请访问GPars website.
- oracle GROUP BY rollup
1.ROW_NUMBER() OVER函数的基本用法用法 http://www.cnblogs.com/fxgachiever/archive/2010/09/15/1826792.html 2.De ...
- Vue全家桶开发笔记
state 中没有属性的情况下,新增属性不会触发mutations修改. 例: commit('change', { c: 3, d: 4, }); state: { test: { a: 1, b: ...