您的位置:首页 > 产品设计 > UI/UE

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];

最后运行工程,效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: