键盘挡住UITextField问题和TPKeyboardAvoiding
来源:互联网 发布:中老年骨质疏松知乎 编辑:程序博客网 时间:2024/04/28 17:33
再正式开始之前,先来介绍一下IOS的键盘类型:
一、键盘风格
UIKit框架支持8种风格键盘
01.typedef enum {
02.UIKeyboardTypeDefault, //默认键盘:支持所有字符
03.UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘
04.UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#等符号
05.UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符
06.UIKeyboardTypeNumberPad, //数字键盘
07.UIKeyboardTypePhonePad, //电话键盘
08.UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名字
09.UIKeyboardTypeEmailAddress, //用于输入电子邮件地址的键盘
10.} UIKeyboardType;
用法用例:
textView.keyboardtype= UIKeyboardTypeNumberPad;
二、键盘外观
1.typedef enum {
2.UIKeyboardAppearanceDefault, //默认外观:浅灰色
3.UIKeyboardAppearanceAlert, //深灰/石墨色
4.} UIKeyboardAppearance;
用法用例:
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
三、回车键
01.typedef enum {
02.UIReturnKeyDefault, //默认:灰色按钮,标有Return
03.UIReturnKeyGo, //标有Go的蓝色按钮
04.UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索
05.UIReturnKeyJoin, //标有Join的蓝色按钮
06.UIReturnKeyNext, //标有Next的蓝色按钮
07.UIReturnKeyRoute, //标有Route的蓝色按钮
08.UIReturnKeySearch, //标有Search的蓝色按钮
09.UIReturnKeySend, //标有Send的蓝色按钮
10.UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
11.UIReturnKeyDone, //标有Done的蓝色按钮
12.UIReturnKeyEmergencyCall, //紧急呼叫按钮
13.} UIReturnKeyType;
用法用例:
textView.returnKeyType=UIReturnKeyGo;
四、自动大写
1.typedef enum {
2.UITextAutocapitalizationTypeNone, //不自动大写
3.UITextAutocapitalizationTypeWords, //单词首字母大写
4.UITextAutocapitalizationTypeSentences, //句子首字母大写
5.UITextAutocapitalizationTypeAllCharacters, //所有字母大写
6.}UITextAutocapitalizationType;
用法用例:textField.autocapitalizationType= UITextAutocapitalizationTypeWords;
五、自动更正
1.typedef enum {
2.UITextAutocorrectionTypeDefault,//默认
3.UITextAutocorrectionTypeNo,//不自动更正
4.UITextAutocorrectionTypeYes,//自动更正
5.} UITextAutocorrectionType;
用法用例:textField.autocorrectionType= UITextAutocorrectionTypeYes
六、安全文本输入
textView.secureTextEntry=YES;
开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。
以上内容都可以在 inspector中设置:
在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘。对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭它。例如,我们可以实现按下 Rerun (有时也是 Done、Research 等)键关闭键盘,或者,更人性化的,轻触背景关闭键盘。
1、首先讲一下按下Return键关闭键盘。
当按下键盘的 Return 键,会产生一个 Did End On Exit 事件,此时,我们告诉文本框要放弃控件,于是键盘就消失了。
假设,我们已经创建了一个 Single View Application ,并打开ViewController.xib 文件,在 View 上拖上去了三个 Text Field ,然后,我们把这三个文本框映射到 ViewController.h 中,名称依次是 firstField、secondField 以及 thirdField 。
(1)在 ViewController.h 中声明一个方法:
1.-(IBAction)textFiledReturnEditing:(id)sender;
(2)在ViewController.m 中实现这个方法:
1.-(IBAction)textFiledReturnEditing:(id)sender{
2.[sender resignFirstResponder];
3.}
让这三个文本框都映射到 textFiledReturnEditing 方法,不过此时的事件应当是 Did End On Exit ,具体操作是:
打开 Assistant Editor ,左边打开ViewController.xib ,右边打开 ViewController.h ,在 Xcode 最右边打开 Connector Inspector ,然后在 View 中选择第一个文本框,在 Connector Inspector 中找到 Did End On Exit ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 textFiledReturnEditing 方法,
点击背景关闭键盘:
让 View 映射到这个方法,不过事先,我们先要改变 View 的类型。
打开xib,选中 View ,打开 Identity Inspector ,在 class 中选择 UIControl :
打开Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在Xcode最右边打开 Connector Inspector ,在 ViewController.xib 中选择 Control ,在Connector Inspector 中找到 TouchDown ,从它右边的圆圈中拉出映射线,映射到ViewController.h 的backgroundTap 方法,
三.解决虚拟键盘挡住UITextField的方法
因为屏幕太小的缘故,一个键盘跳出来总是把输入框挡住,所以需要移动屏幕来匹配键盘
01.#pragma mark -
02.#pragma mark解决虚拟键盘挡住UITextField的方法
03.- (void)keyboardWillShow:(NSNotification*)noti
04.{
05.//键盘输入的界面调整
06.//键盘的高度
07.float height = 216.0;
08.CGRect frame =self.view.frame;
09.frame.size =CGSizeMake(frame.size.width, frame.size.height -height);
10.[UIView beginAnimations:@"Curl"context:nil];//动画开始
11.[UIView setAnimationDuration:0.30];
12.[UIView setAnimationDelegate:self];
13.[self.view setFrame:frame];
14.[UIView commitAnimations];
15.}
16.
17.-(BOOL)textFieldShouldReturn:(UITextField*)textField
18.{
19.// When the user presses return, takefocus away from the text field so that the keyboard isdismissed.
20.NSTimeInterval animationDuration= 0.30f;
21.[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
22.[UIView setAnimationDuration:animationDuration];
23.CGRect rect = CGRectMake(0.0f, 0.0f,self.view.frame.size.width, self.view.frame.size.height);
24.//CGRect rect = CGRectMake(0.0f,20.0f, self.view.frame.size.width, self.view.frame.size.height);
25.self.view.frame = rect;
26.[UIView commitAnimations];
27.[textField resignFirstResponder];
28.return YES;
29.}
30.
31.- (void)textFieldDidBeginEditing:(UITextField*)textField
32.{
33.CGRect frame = textField.frame;
34.int offset = frame.origin.y+ 32 -(self.view.frame.size.height - 216.0);//键盘高度216
35.NSTimeInterval animationDuration = 0.30f;
36.[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
37.[UIView setAnimationDuration:animationDuration];
38.float width =self.view.frame.size.width;
39.float height =self.view.frame.size.height;
40.if(offset > 0)
41.{
42.CGRect rect = CGRectMake(0.0f,-offset,width,height);
43.self.view.frame =rect;
44.}
45.[UIView commitAnimations];
46.}
47.#pragma mark -
只要在代码中加入这三个文件,然后将自身delegate
控制器添加UITextFieldDelegate
1.
@interface
ViewController :UIViewController<UITextFieldDelegate>
在viewDidLoad中添加:
- (void)viewDidLoad
02.{
03.[super viewDidLoad];
04.self.firstTextField.delegate=self;
05.
06.self.secondTextField.delegate=self;
07.self.thirdTextField.delegate=self;
但是这里经常会有屏幕移动后不能返回的问题,这里的解决方案就是
-(IBAction)backgroundTap:(id)sender {
02.[self.firstTextField resignFirstResponder];
03.[self.secondTextField resignFirstResponder];
04.[self.thirdTextField resignFirstResponder];
05.NSTimeInterval animationDuration = 0.30f;
06.[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
07.[UIView setAnimationDuration:animationDuration];
08.CGRect rect = CGRectMake(0.0f, 0.0f,self.view.frame.size.width, self.view.frame.size.height);
09.self.view.frame = rect;
10.
11.}
在backgroundTap函数中添加这些代码,这样屏幕就会返回正常了。
例子下载链接http://www.it165.net/uploadfile/files/2014/0303/KayBoardDemo.zip
避免键盘遮挡下方输入框--TPKeyboardAvoiding (在iOS代码库中浏览本帖)
- 关键字:键盘,工具栏,UITextField,Keyboard
- 代码类库:键盘(Keyboard)
- GitHub链接https://github.com/michaeltyson/TPKeyboardAvoiding
TPKeyboardAvoidingScrollView用于在scrollview中实现自动上滚,避免键盘遮盖住了textfield,textview等控件。
使用方法:
将TPKeyboardAvoidingScrollView.h和TPKeyboardAvoidingScrollView.m add进工程中
在需要用键盘自动上滚的controller的xib文件中,找到view,修改其Class为TPKeyboardAvoidingScrollView就可以了
源码:http://download.csdn.net/detail/chaoyuan899/6309641
- 键盘挡住UITextField问题和TPKeyboardAvoiding
- ios 解决键盘挡住UITextField的问题
- UITextView, UITextField输入内容, 键盘挡住问题
- 不让键盘挡住UITextField
- 不让键盘挡住UITextField
- 不让键盘挡住UITextField
- iOS 键盘挡住UITextField
- 解决iOS开发中防止键盘挡住UITextField问题
- UITextField和UITextView被键盘挡住的处理办法
- ios7学习之路七(隐藏虚拟键盘,解决键盘挡住UITextField问题)
- IOS 不让键盘挡住UITextField (自适应)
- 如何不让键盘挡住UITextField控件
- 如何不让键盘挡住UITextField控件
- UI -- 虚拟键盘挡住UITextField的解决方法
- 如何不让键盘挡住UITextField控件
- iOS 虚拟键盘挡住UITextField的解决方法
- UITextField被键盘挡住的解决办法
- 如何不让键盘挡住UITextField控件
- 深入浅出Mybatis-改造Cache
- Spring学习笔记(5)----依赖注入的简单实现
- 安卓图片压缩丑的根源----十年前Google的错误
- 权限修饰符的使用与比较
- http://daikainan.iteye.com/blog/1405575
- 键盘挡住UITextField问题和TPKeyboardAvoiding
- easyui Datagrid的deleteRow多行移除问题
- Spring学习笔记(6)----编码剖析Spring依赖注入的原理
- 推荐算法
- postgreSQL 使用With字句的例子
- GitHub使用教程
- 《openstack-nova》use-novaclient 创建虚拟机(createvms.py)
- Android学习 8-> 相对布局RelativeLayout
- SQLSERVER创建维护计划失败时解决办法