iPhone键盘自定义及通知
一.键盘通知
当文本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为
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}}";
}
二.文本对象与WebView键盘设置
UITextFiled和 UITextView都遵循 UITextInputTraits协议,在UITextInputTraits协议中定义了设置键盘的属性,有
2. returnKeyType:键盘Return键显示的文本,默认为”Return”,其他可选择的包括Go,Next,Done,Send,Google等。
若想显示黑色键盘又不想透明露出底部视图,可以将键盘配置成Alert类型的,然后监听键盘显示的广播通知,在显示键盘时在键盘底部增加一不透明黑色背景视图。
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>
三. 使用键盘通知调整文本视图位置
其流程为
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];}
// 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。
四.使用inputAccessoryView与inputView定制输入视图
自定义inputAccessoryView非常常见,如编辑短信时的输入框
iPhone键盘自定义及通知相关推荐
- iPhone键盘通知与键盘定制
一.键盘通知 当文本View(如UITextField,UITextView, UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstres ...
- iphone控制中心自定义没有计算机,如何在iPhone上自定义iOS 11控制中心功能
苹果最新推出的适用于iPhone和iPad的iOS 11,具有重新设计和模块化的控制中心.这个新的控制中心具有相当多的3D触摸手势,新的动画和可定制性.此外,内置了新的控件,以前在iOS版本下载或使用 ...
- iphone 模拟滑动_如何在iPhone键盘上禁用滑动输入
iphone 模拟滑动 Justin Duino 贾斯汀·杜伊诺(Justin Duino) Android has supported swipe keyboards for over half a ...
- iphone 快捷 自定义_通过此快捷方式在iPhone上快速键入数字和符号
iphone 快捷 自定义 Normally, the default iOS keyboard presents you with the alphabet. When you want to ac ...
- reddit_如何使用Python创建自定义Reddit通知系统
reddit by Kelsey Wang 王凯西 如何使用Python创建自定义Reddit通知系统 (How to make a custom Reddit notification system ...
- iOS 10 的一个重要更新-自定义的通知界面
续上篇,在简单闹钟的例子上,在通知界面上显示图片动画,并用通知关联的按钮更新通知界面.介绍 iOS 10 通知 API 的扩展:自定义通知显示界面. 新框架可以统一处理本地通知和远程推送,同时增加了一 ...
- 【Win10应用开发】自定义磁贴通知的排版
[Win10应用开发]自定义磁贴通知的排版 原文:[Win10应用开发]自定义磁贴通知的排版 前面老周用了两篇烂文,向大家介绍了Adaptive磁贴的模板使用.那些XML模板已经很强大了,不过,如果你 ...
- android notification应用之自定义来电通知
android notification应用之自定义来电通知 1.为了实现老板的各种要求 本人矜矜业业完成任务 随着这个软电话软件的日益完善 本来来电的时候是创建一条通知点亮屏幕 用户可以解锁屏幕后接 ...
- 如何在 Android 上自定义来电通知?带有代码示例
您将从本文中学习如何在 Android 上进行从基本布局到高级布局的来电通知.使用我们的示例自定义通知屏幕. 今天,我们将处理来电通知:我们将从最简单和最简约的通知开始,并以采用非系统设计的全屏通知结 ...
最新文章
- 007_Checkbox多选框
- 常用PHP array数组函数
- Linux如何访问mmio空间,一文读懂Linux下如何访问I/O端口和I/O内存
- 未能比较数组中的两个元素_算法3 寻找两个正序数组的中序数
- Anti-Screen Capture(Prevent Screen Captures)截屏与反截屏
- unittest测试框架_python接口自动化测试 - 4.unittest单元测试框架学习
- angularjs上传文件到服务器,AngularJS:如何使用multipart表单实现简单的文件上传?...
- windows封装/备份恢复/双系统安装
- android 多行排列,安卓简单布局样例_采用LinearLayout实现多列多行展示
- Python学习02 列表 List
- Des加密后传参被特殊字符(如+)截断
- XGBOOST_航班延误预测
- 13行列式02---余子式与代数余子式、行列式按行(列)展开法则、行列式计算、范德蒙行列式
- 华三交换机ping大包命令_华三(H3C)交换机命令大全
- 英语语法 - the + 形容词 的意义
- 第2章第29节:英文排版技巧:无衬线字体和手写字体的搭配 [PowerPoint精美幻灯片实战教程]
- sdn网络搭建以及负载均衡
- 中国中学生击剑锦标赛
- 【计算机毕业设计】251毕业论文管理系统设计与实现
- electron打包的一些问题