ios开发经验程序员半栈工程师

IOS键盘输入屏幕上移

2017-12-12  本文已影响60人  无邪8

在IOS开法中经常会遇到键盘遮挡屏幕的事情(比如输入账号密码验证码等等),就使得原本都不大的屏幕直接占了一半甚至更多的位置,这倒无所谓,关键是挡住了下面的按钮。这样的话按钮的事件也就触发不了,最好的解决办法就是当输入这些信息的时候让整个屏幕上移一个键盘的位置,或者上移到指定的位置。

因为不是只有登录注册界面才会有这个问题,所以最好把这种声明写到基类里。然后所有的controller都继承基类。

一、

首先一般输入的话都用的是UITextField,所以要监听用户什么时候开始输入和什么时候结束输入,直接设置代理代理就行了,要遵受

UITextFieldDelegate协议。

//遵循协议

@interfaceViewController()

//代理方法

#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

return [textField resignFirstResponder];

}

//键盘弹出时屏幕上移

-(void)textFieldDidBeginEditing:(UITextField*)textField

{

//假如多个输入,比如注册和登录,就可以根据不同的输入框来上移不同的位置,从而更加人性化

//键盘高度216

//滑动效果(动画)

CGRect frame = textField.frame;

int offset = frame.origin.y + 32 - (HEIGHT- 280.0);//键盘高度216

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

[UIView setAnimationDuration:animationDuration];

if(offset > 0)//加上屏幕上移textField是否会移除屏幕的判断

{

CGRect rect = CGRectMake(0.0f, -offset,WIDTH,HEIGHT);

self.view.frame = rect;

}

[UIView commitAnimations];

}

//取消第一响应,也就是输入完毕,屏幕恢复原状

-(void)textFieldDidEndEditing:(UITextField*)textField

{

//滑动效果

NSTimeIntervalanimationDuration =0.30f;

[UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];

[UIViewsetAnimationDuration:animationDuration];

//恢复屏幕

self.view.frame=CGRectMake(0.0f,0.0f,self.view.frame.size.width,self.view.frame.size.height);

[UIViewcommitAnimations];

}


二、

也可以用给键盘kvc实现功能

在基类的viewWillAppear方法中注册监听。

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self setupForKeyBoard];

}

#pragma mark -键盘相关

//键盘的准备

- (void)setupForKeyBoard

{

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

//点击手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyBoardHideAction)];

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

//键盘显示后添加手势

[nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {

[self.view addGestureRecognizer:tap];

[self keyboardWillShow:note];

}];

//键盘消失后移除手势

[nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {

[self.view removeGestureRecognizer:tap];

//键盘动画时间

double duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

self.view.frame = CGRectMake(0,  0 , WIDTH, HEIGHT);

}];

}];

}

//键盘消失后view下移

- (void)keyBoardHideAction

{

[self.view endEditing:YES];

}

//通过note获取键盘高度,键盘动画时间

- (void)keyboardWillShow:(NSNotification *)note

{

//获取键盘高度,在不同设备上,以及中英文下是不同的

CGFloat keyboardH = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

//计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)

CGFloat offset = (self.viewBottom+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight);

//键盘动画时间

double duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

if (offset > 0){

[UIView animateWithDuration:duration animations:^{

self.view.frame = CGRectMake(0, - offset ), WIDTH, HEIGHT);

}];

}

#pragma mark - UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView

{

//将textField的rect转换到self.view上

CGRect rect = [textView.superview convertRect:textView.frame toView:self.view];

//textField的底部

self.viewBottom = rect.origin.y + rect.size.height;

}

- (void)textViewDidEndEditing:(UITextView *)textView

{

[UIView animateWithDuration:0.25 animations:^{

self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);

}];

}

上一篇 下一篇

猜你喜欢

热点阅读