ios 开发 键盘 android,iOS开发-键盘正确的弹出姿势

最近对项目中的键盘弹出做了一点优化,在此分享。

思考:正常的键盘弹出姿势是什么样的?是否只需要向上移动适当的高度满足用户操作即可。我认为应该是对于键盘没有挡住输入框则无需让视图向上移动,键盘挡住了输入框则让输入移动至键盘上方即可。

GIF如下:

3049694bc569

keyboard.gif

demo 视图层次说明

baseView,firstTextfield,secodeTextfield三个视图

3049694bc569

baseView.png

思路

弹出键盘时,挡住当前响应的控件则视图上移,未挡住则不上移

3049694bc569

description.png

核心代码

监听键盘弹出隐藏

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//监听键盘frame改变

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

//键盘将要弹出

- (void)keyboardWillShow:(NSNotification *)notification

{

//获取键盘高度 keyboardHeight

NSDictionary *userInfo = [notification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

CGFloat keyboardHeight = keyboardRect.size.height;

//当前屏幕高度,无论横屏竖屏

CGFloat screenH = [UIScreen mainScreen].bounds.size.height;

//主窗口

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

//获取当前响应者

UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

NSLog(@"firstResponder:%@",firstResponder);

// Twitter 网络登录时上移问题

Class clName = NSClassFromString(@"UIWebBrowserView");

if ([firstResponder isKindOfClass:clName]) return;

if (![firstResponder respondsToSelector:@selector(center)]) return;

//当前响应者在其父视图的中点(x居中 y最下点)

CGPoint firstCPoint = CGPointMake(firstResponder.center.x, CGRectGetMaxY(firstResponder.frame));

//当前响应者在屏幕中的point

CGPoint convertPoint = [keyWindow convertPoint:firstCPoint fromView:firstResponder.superview];

//[firstResponder convertPoint:firstCPoint toView:keyWindow];

//当前响应者的最大y值

CGFloat firstRespHeight = convertPoint.y;

//键盘最高点的y值

CGFloat topHeighY = screenH - keyboardHeight;//顶部高度的Y值

if (topHeighY < firstRespHeight) { //键盘挡住了当前响应的控件 需要上移

CGFloat spaceHeight = firstRespHeight - topHeighY;

self.baseView.center = CGPointMake(self.baseView.center.x, self.baseView.center.y - spaceHeight);

CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

[self.view layoutIfNeeded];

}];

}else{

NSLog(@"键盘未挡住输入框");

}

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

//结束编辑

[self.view endEditing:YES];

}

//键盘将要隐藏

- (void)keyboardWillHide:(NSNotification *)notification{

self.baseView.center = self.view.center;

NSDictionary *userInfo = [notification userInfo];

CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

[self.view layoutIfNeeded];

}];

}

去掉警告⚠️

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Wundeclared-selector"

UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

#pragma clang diagnostic pop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值