如何处理键盘遮挡UITextField
2016-04-25 11:37
441 查看
#import "ViewController.h"
@interface
ViewController ()<UITextFieldDelegate>
@property (weak,
nonatomic) IBOutletUITextField *name;
@property (weak,
nonatomic) IBOutletUITextField *password;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//指定本身为代理
self.name.delegate =self;
self.password.delegate =self;
//指定编辑时键盘的return键类型
self.name.returnKeyType
= UIReturnKeyNext;
self.password.returnKeyType
= UIReturnKeyDefault;
//注册键盘响应事件方法
[self.nameaddTarget:selfaction:@selector(nextOnKeyboard:)forControlEvents:UIControlEventEditingDidEndOnExit];
[self.passwordaddTarget:selfaction:@selector(nextOnKeyboard:)forControlEvents:UIControlEventEditingDidEndOnExit];
//添加手势,点击屏幕其他区域关闭键盘的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizeralloc]
initWithTarget:selfaction:@selector(hidenKeyboard)];
gesture.numberOfTapsRequired =1;
[self.viewaddGestureRecognizer:gesture];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UITextField的协议方法,当开始编辑时监听
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSTimeInterval animationDuration=0.30f;
[UIViewbeginAnimations:@"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,-200,width,height);
self.view.frame=rect;
[UIViewcommitAnimations];
return
YES;
}
//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIViewbeginAnimations:@"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 = 20.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIViewcommitAnimations];
}
//隐藏键盘的方法
-(void)hidenKeyboard
{
[self.nameresignFirstResponder];
[self.passwordresignFirstResponder];
[selfresumeView];
}
//点击键盘上的Return按钮响应的方法
-(void)nextOnKeyboard:(UITextField *)sender
{
if (sender ==
self.name) {
[self.passwordbecomeFirstResponder];
}else if (sender ==self.password){
[selfhidenKeyboard];
}
}
@end
@interface
ViewController ()<UITextFieldDelegate>
@property (weak,
nonatomic) IBOutletUITextField *name;
@property (weak,
nonatomic) IBOutletUITextField *password;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//指定本身为代理
self.name.delegate =self;
self.password.delegate =self;
//指定编辑时键盘的return键类型
self.name.returnKeyType
= UIReturnKeyNext;
self.password.returnKeyType
= UIReturnKeyDefault;
//注册键盘响应事件方法
[self.nameaddTarget:selfaction:@selector(nextOnKeyboard:)forControlEvents:UIControlEventEditingDidEndOnExit];
[self.passwordaddTarget:selfaction:@selector(nextOnKeyboard:)forControlEvents:UIControlEventEditingDidEndOnExit];
//添加手势,点击屏幕其他区域关闭键盘的操作
UITapGestureRecognizer *gesture = [[UITapGestureRecognizeralloc]
initWithTarget:selfaction:@selector(hidenKeyboard)];
gesture.numberOfTapsRequired =1;
[self.viewaddGestureRecognizer:gesture];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UITextField的协议方法,当开始编辑时监听
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSTimeInterval animationDuration=0.30f;
[UIViewbeginAnimations:@"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,-200,width,height);
self.view.frame=rect;
[UIViewcommitAnimations];
return
YES;
}
//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIViewbeginAnimations:@"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 = 20.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIViewcommitAnimations];
}
//隐藏键盘的方法
-(void)hidenKeyboard
{
[self.nameresignFirstResponder];
[self.passwordresignFirstResponder];
[selfresumeView];
}
//点击键盘上的Return按钮响应的方法
-(void)nextOnKeyboard:(UITextField *)sender
{
if (sender ==
self.name) {
[self.passwordbecomeFirstResponder];
}else if (sender ==self.password){
[selfhidenKeyboard];
}
}
@end
相关文章推荐
- IOS UIFont 字体大全
- 在UEFI模式下安装Ubuntu(一)——制作UEFI模式启动U盘
- using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
- 【SystemUI】修改下拉状态栏颜色
- HDU 5667 Sequence
- iOS中Xcode使用UIScrollView+AutoLayout轻松实现滚动布局
- iOS 通过UIDevice 获取系统相关属性
- 个性化 UIAlertController
- 【CodeForces 618B 】Guess the Permutation(水题)
- WM_CLOSE,WM_QUIT,WM_DESTROY消息的区别
- 解决StoryBoard上cell上定义的view的背景点击后不显示的问题(其中用uiview 当作线的时候最多遇到,会不显示线 而出现不流畅问题)
- UICollectionView 基础篇
- LeetCode 64/62/63. Minimum Path Sum/ Unique Paths i, ii
- UILabel文字重叠
- Quick Sort
- 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成
- hdu 3836 Equivalent Sets //tarjan+缩点
- 2016.04.25,英语,《Vocabulary Builder》Unit 18
- Mavlink地面站编写之五-Mission Planner中ProgressReporterDialogue和读串口线程serialreaderthread的分析
- JAVA--建造者模式(Builder)--设计模式四