iOS -键盘显示与隐藏
来源:互联网 发布:vb安装教程 编辑:程序博客网 时间:2024/04/29 00:58
目前我所接触到的键盘处理方法有两种,第一种是通过约束,第二种是通过transform。
// 监听键盘通知 -(void)viewDidLoad{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; }
<pre name="code" class="objc"> - (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}
#pragma mark - 键盘处理- (void)keyboardWillShow:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改约束 self.bottomSpacing.constant = rect.size.height; [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }];}- (void)keyboardWillHide:(NSNotification *)note { // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改约束 self.bottomSpacing.constant = 0; [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }];}
上面是第一种方法,简单来说就是通过底部约束来达到这样一个效果。
// 监听键盘通知 -(void)viewDidLoad{ // 监听键盘通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 键盘处理- (void)keyboardWillChangeFrame:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改transform [UIView animateWithDuration:duration animations:^{ CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y; self.view.transform = CGAffineTransformMakeTranslation(0, - ty); }];}第二种方法就是通过transform将整个屏幕的view向上移动或向下移动一个键盘的高度。
(如果有新方法欢迎回帖
0 0
- iOS -键盘显示与隐藏
- ios 利用通知处理键盘的显示与隐藏
- iOS中键盘显示与隐藏的套路模版
- 键盘的隐藏与显示
- Android键盘显示与隐藏
- iOS开发中的键盘显示和隐藏
- Android 键盘的显示与隐藏
- android系统键盘隐藏与显示问题
- 系统键盘调用 显示与隐藏
- phonegap 对键盘的隐藏与显示
- Android 虚拟键盘的显示与隐藏
- android 键盘的隐藏与显示
- Android键盘的显示与隐藏
- android 隐藏键盘 显示键盘
- 隐藏键盘和显示键盘
- 显示、隐藏虚拟键盘
- 键盘显示隐藏控制
- 键盘显示隐藏
- 网络面试题整理
- 等等!我改變主意了! 如何在狀態機轉換過程時中斷
- cJSON 解析器开源项目学习笔记
- 使用FAAD库解码AAC实例
- HttpSessionBindingListener的使用
- iOS -键盘显示与隐藏
- NGINX不支持SLIM访问模式问题
- AngularJS Directive 隔离Scope数据交互
- 关于++运算符在java与c里面的区别
- 抓包工具介绍、手机抓包、wireshark使用技巧
- C#图形工具
- Android多种多渠道打包工具对比
- VS 修改输出路径和文件名
- Fixed Objects Statistics and why they are important