ios--解决键盘遮挡UITextField控件的问题(方式一)
2014-03-19 16:48
671 查看
参考资料:http://blog.csdn.net/ryantang03/article/details/8203605
主要功能包括:
自适应键盘出现后View的高度调整,防止遮挡输入框
点击背景区域关闭键盘
响应键盘上Return按钮事件(实现在上下UITextFiled间切换光标)
1.首先在ViewController中实现UITextField的一个Delegate
#import <UIKit/UIKit.h>
@interface AloneSetSiteViewController : UIViewController<UITextFieldDelegate>
{
}
@property(nonatomic,retain) NSMutableDictionary *dictionary;
@property(nonatomic,retain) IBOutlet UITextField *txtName,*txtPerson,*txtCircle,*txtPassWd,*txtConfirmPassWd;
@property(nonatomic,retain) IBOutlet UIScrollView *scroller;
-(IBAction)backOff:(id)sender;
-(IBAction)nextStep:(id)sender;
@end
2.实现UITextFiledDelegate中的协议方法
//UITextField的协议方法,当开始编辑时监听
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
float upDist = 0.0f;
if (textField == txtCircle) {
upDist = -40.0f;
}
else if (textField == txtPassWd) {
upDist = -120.0f;
}
else if(textField == txtConfirmPassWd)
{
upDist = -200.0f;
}
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//上移30个单位,按实际情况设置
CGRect rect=CGRectMake(0.0f,upDist,width,height);
self.view.frame=rect;
[UIView commitAnimations];
return YES;
}
3.恢复移动的视图的方法
//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度
float Y = 0.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIView commitAnimations];
}
4.点击背景隐藏键盘及响应键盘上Return按键的方法
//隐藏键盘的方法
-(void)hidenKeyboard
{
[self.txtName resignFirstResponder];
[self.txtCircle resignFirstResponder];
[self.txtPerson resignFirstResponder];
[self.txtPassWd resignFirstResponder];
[self.txtConfirmPassWd resignFirstResponder];
[self resumeView];
}
//点击键盘上的Return按钮响应的方法
-(IBAction)nextOnKeyboard:(UITextField *)sender
{
if(sender == self.txtName) {
[self.txtPerson becomeFirstResponder];
}
else if(sender == self.txtPerson) {
[self.txtCircle becomeFirstResponder];
}
else if(sender == self.txtCircle) {
[self.txtPassWd becomeFirstResponder];
}
else if (sender == self.txtPassWd) {
[self.txtConfirmPassWd becomeFirstResponder];
}
else if (sender == self.txtConfirmPassWd){
[self hidenKeyboard];
}
}
5.指定协议及注册事件
/// 注册键盘事件
//指定本身为代理
self.txtName.delegate = self;
self.txtCircle.delegate = self;
self.txtPerson.delegate = self;
self.txtPassWd.delegate = self;
self.txtConfirmPassWd.delegate = self;
//指定编辑时键盘的return键类型
self.txtName.returnKeyType = UIReturnKeyNext;
self.txtCircle.returnKeyType = UIReturnKeyNext;
self.txtPerson.returnKeyType = UIReturnKeyNext;
self.txtPassWd.returnKeyType = UIReturnKeyNext;
self.txtConfirmPassWd.returnKeyType = UIReturnKeyDefault;
//注册键盘响应事件方法
[self.txtName addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtCircle addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtPerson addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtConfirmPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
//添加手势,点击屏幕其他区域关闭键盘的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
gesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:gesture];
最后运行工程,效果如下:
主要功能包括:
自适应键盘出现后View的高度调整,防止遮挡输入框
点击背景区域关闭键盘
响应键盘上Return按钮事件(实现在上下UITextFiled间切换光标)
1.首先在ViewController中实现UITextField的一个Delegate
#import <UIKit/UIKit.h>
@interface AloneSetSiteViewController : UIViewController<UITextFieldDelegate>
{
}
@property(nonatomic,retain) NSMutableDictionary *dictionary;
@property(nonatomic,retain) IBOutlet UITextField *txtName,*txtPerson,*txtCircle,*txtPassWd,*txtConfirmPassWd;
@property(nonatomic,retain) IBOutlet UIScrollView *scroller;
-(IBAction)backOff:(id)sender;
-(IBAction)nextStep:(id)sender;
@end
2.实现UITextFiledDelegate中的协议方法
//UITextField的协议方法,当开始编辑时监听
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
float upDist = 0.0f;
if (textField == txtCircle) {
upDist = -40.0f;
}
else if (textField == txtPassWd) {
upDist = -120.0f;
}
else if(textField == txtConfirmPassWd)
{
upDist = -200.0f;
}
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//上移30个单位,按实际情况设置
CGRect rect=CGRectMake(0.0f,upDist,width,height);
self.view.frame=rect;
[UIView commitAnimations];
return YES;
}
3.恢复移动的视图的方法
//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度
float Y = 0.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIView commitAnimations];
}
4.点击背景隐藏键盘及响应键盘上Return按键的方法
//隐藏键盘的方法
-(void)hidenKeyboard
{
[self.txtName resignFirstResponder];
[self.txtCircle resignFirstResponder];
[self.txtPerson resignFirstResponder];
[self.txtPassWd resignFirstResponder];
[self.txtConfirmPassWd resignFirstResponder];
[self resumeView];
}
//点击键盘上的Return按钮响应的方法
-(IBAction)nextOnKeyboard:(UITextField *)sender
{
if(sender == self.txtName) {
[self.txtPerson becomeFirstResponder];
}
else if(sender == self.txtPerson) {
[self.txtCircle becomeFirstResponder];
}
else if(sender == self.txtCircle) {
[self.txtPassWd becomeFirstResponder];
}
else if (sender == self.txtPassWd) {
[self.txtConfirmPassWd becomeFirstResponder];
}
else if (sender == self.txtConfirmPassWd){
[self hidenKeyboard];
}
}
5.指定协议及注册事件
/// 注册键盘事件
//指定本身为代理
self.txtName.delegate = self;
self.txtCircle.delegate = self;
self.txtPerson.delegate = self;
self.txtPassWd.delegate = self;
self.txtConfirmPassWd.delegate = self;
//指定编辑时键盘的return键类型
self.txtName.returnKeyType = UIReturnKeyNext;
self.txtCircle.returnKeyType = UIReturnKeyNext;
self.txtPerson.returnKeyType = UIReturnKeyNext;
self.txtPassWd.returnKeyType = UIReturnKeyNext;
self.txtConfirmPassWd.returnKeyType = UIReturnKeyDefault;
//注册键盘响应事件方法
[self.txtName addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtCircle addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtPerson addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.txtConfirmPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
//添加手势,点击屏幕其他区域关闭键盘的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
gesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:gesture];
最后运行工程,效果如下:
相关文章推荐
- ios--解决键盘遮挡UITextField控件的问题(方式二)
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- iOS开发笔记之解决键盘遮挡UITextField问题
- 『零行代码』解决键盘遮挡问题(iOS)
- 解决键盘遮挡UITextField问题
- iOS解决呼出键盘遮挡页面问题(装逼模式一)
- 解决键盘遮挡输入框(UITextField)问题
- ios解决webview键盘遮挡问题
- 『零行代码』解决键盘遮挡问题(iOS)
- 解决键盘遮挡输入框(UITextField)问题
- Android实现登陆页logo随键盘收放动态伸缩(完美解决键盘弹出遮挡控件的问题)
- UIScrollView中的UITextField键盘遮挡问题的最简单解决办法
- iOS解决键盘遮挡问题
- iOS隐藏键盘实现和针对键盘遮挡输入控件的解决