首页 / IOS / ios 中键盘被遮挡解决方案
ios 中键盘被遮挡解决方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ios 中键盘被遮挡解决方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3232字,纯文字阅读大概需要5分钟。
内容图文
![ios 中键盘被遮挡解决方案](/upload/InfoBanner/zyjiaocheng/1320/e34682c8f8484a11b71ae7c5b90f1e96.jpg)
1.当view是非可以滚动的view时,
// 添加对键盘的通知 - -(void)viewDidLoad{ [[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 *)sender{ { // 得到键盘的高度 NSDictionary *dict = [sender userInfo]; CGSize keyboardSize = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size; // 得到输入框CGRectGetMaxY(<#CGRect rect#>)+40到底部的高度 CGFloat height = (SCREEN_H - (_IPText.frame.origin.y + _IPText.frame.size.height + 40)); CGFloat newY = -keyboardSize.height + height; // 如果键盘的高度大于上面的高度 if (keyboardSize.height > height) { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, newY, SCREEN_W, SCREEN_H); [UIView commitAnimations]; }
} - (void)keyboardWillHide:(NSNotification *)sender { [UIView beginAnimations:nil context:NULL]; // 动画时间3s [UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H); [UIView commitAnimations]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [_IPText resignFirstResponder]; }
2.当view时可滚动的view时。
- (void)viewDidLoad { // 监听键盘的弹出与隐藏 // 利用消息中心,首先监听UIKeyboardWillChangeFrameNotification 键盘frame改变的消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } //监听键盘弹出与退出的方法 -(void)keyboardChangeFrame:(NSNotification *)sender { /* UIKeyboardAnimationCurveUserInfoKey = 7; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}"; //弹出键盘 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}"; //隐藏键盘 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 216}}"; */ //transform是相对位移 // 设置窗口的颜色 self.view.window.backgroundColor = self.tableView.backgroundColor; // 0.取出键盘动画的时间 CGFloat duration = [sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 1.取得键盘最后的frame CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 2.计算控制器的view需要平移的距离 CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height; // 3.让UITableView的最后一个cell滚到键盘最上面 // self.view.transform = CGAffineTransformMakeTranslation(0, transformY); NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messagesFrame.count - 1 inSection:0]; [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES]; // 4.执行动画(self.view.transform是tableView加上textFiled一起向上移动,也就是整个view) [UIView animateWithDuration:duration animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, transformY); }]; // self.view.transform = CGAffineTransformMakeTranslation(0, -216); }
原文:http://www.cnblogs.com/peaker-wu/p/5417777.html
内容总结
以上是互联网集市为您收集整理的ios 中键盘被遮挡解决方案全部内容,希望文章能够帮你解决ios 中键盘被遮挡解决方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。