iOS开发之监听键盘高度的变化
最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又不会被键盘挡住。
下面是我实现的方法:(利用通知)
// 键盘通知
// 键盘的frame发生改变时发出的通知(位置和尺寸)
// UIKeyboardWillChangeFrameNotification
// UIKeyboardDidChangeFrameNotification
// 键盘显示时发出的通知
// UIKeyboardWillShowNotification
// UIKeyboardDidShowNotification
// 键盘隐藏时发出的通知
// UIKeyboardWillHideNotification
// UIKeyboardDidHideNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//在这里注册通知
下面是监听通知:
pragma mark - 监听方法
/**
- 键盘的frame发生改变时调用(显示、隐藏等)
*/
(void)keyboardWillChangeFrame:(NSNotification )notification
{
// if (self.picking) return;
/*
notification.userInfo = @{
// 键盘弹出\隐藏后的frame
UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 352}, {320, 216}},
// 键盘弹出\隐藏所耗费的时间
UIKeyboardAnimationDurationUserInfoKey = 0.25,
// 键盘弹出\隐藏动画的执行节奏(先快后慢,匀速)
UIKeyboardAnimationCurveUserInfoKey = 7
}
*/NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
doubleduration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];// 执行动画
[UIView animateWithDuration:duration animations:^{
// 工具条的Y值 == 键盘的Y值 - 工具条的高度
if(keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度
self.toolbar.y = self.view.height - self.toolbar.height;//这里的self.toolbar就是我的输入框。}else{
self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
}
}];
}
当然,这里我为UIView写了一个类别,实现如下:
.h文件中声明
@interfaceUIView (Extension)
@property(nonatomic, assign) CGFloat x;
@property(nonatomic, assign) CGFloat y;
@property(nonatomic, assign) CGFloat width;
@property(nonatomic, assign) CGFloat height;
@property(nonatomic, assign) CGFloat centerX;
@property(nonatomic, assign) CGFloat centerY;
@property(nonatomic, assign) CGSize size;
@property(nonatomic, assign) CGPoint origin;
@end
.m文件中实现(重写setter 和 getter)
@implementationUIView (Extension)
(void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}(void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}(CGFloat)x
{
returnself.frame.origin.x;
}(CGFloat)y
{
returnself.frame.origin.y;
}(void)setCenterX:(CGFloat)centerX
{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}(CGFloat)centerX
{
returnself.center.x;
}(void)setCenterY:(CGFloat)centerY
{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}(CGFloat)centerY
{
returnself.center.y;
}(void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}(void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}(CGFloat)height
{
returnself.frame.size.height;
}(CGFloat)width
{
returnself.frame.size.width;
}(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}(CGSize)size
{
returnself.frame.size;
}(void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}(CGPoint)origin
{
returnself.frame.origin;
}
@end
有需要的朋友可以直接用
iOS开发之监听键盘高度的变化的更多相关文章
- iOS开发之监听键盘高度的变化 分类: ios技术 2015-04-21 12:04 233人阅读 评论(0) 收藏
最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...
- iOS 监听键盘高度,输入框上升
//设置输入框 ---<因为输入框用了get方法,所以第一次调用输入框要用self 调用>: self.textlab.frame=CGRectMake(, , , ); _textlab ...
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- IOS开发-cell的动态高度
tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方.现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell 首先要准备两个模型,一个是存放数据的 ...
- iOS开发 准确计算Coretext高度
- (int)getAttributedStringHeightWithString:(NSAttributedString *) string WidthValue:(int) width{ ...
- iOS开发--改变tableHeaderView的高度
1.先获取tableHeaderView 2.设置它的frame 3.将该view设置回tableview UIView *view=tableView. tableHeaderView; view. ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
随机推荐
- scrollView的bounds
如果scrollView的contentoffset为(100,0) 那么scrollView的bounds就是(100,y,w,h)
- linux视频学习7(ssh, linux启动过程分析,加解压缩,java网络编程)
回顾数据库mysql的备份和恢复: show databases; user spdb1; show tables; 在mysql/bin目录下 执行备份: ./mysqldump -u root - ...
- 3d 人物残像
前言: gameunity 框架 还在继续完善中,0.2版本将会是一次 重大的里程碑. 正文: 哈哈,大家一定流汗了吧.请原谅我为自己代言. 好了,今天我要讲的是 3d人物残像. 首先我来说下 目前 ...
- PoolManager 简单使用
如图,创建空物体并命名 PoolManager,添加 脚本 spawn pool,并添加 如上图右边一些 参数,prefab为 Resources中 的预置体. 添加 空物体并 改名,如上图,并添加脚 ...
- Android应用程序基础
Android程序使用Java语言编写.Android开发套件把数据.资源文件和Java代码编译到一个.以.apk为后缀名的Android压缩包中.一个单独的apk文件中的所有代码被认为是一个andr ...
- Android Camera Api的心得
(一) 前言最近看Camera的api,觉得写的真的不错.现在翻译过来,给大家分享分享,译文可能不太好,大家将就着看哈. (二) 正文1. CameraCamera是Android framework ...
- 一段代码详解JavaScript面向对象
(function(){ //私有静态成员 var user = ""; //私有静态方法 function privateStaticMethod(){ } Box = func ...
- MJRefresh
automaticallyChangeAlpha 下拉或上拉时,文字颜色逐渐加深
- 开发板S3C2440挂起NFS步骤
第一.安装.配置.启动FTP.SSH或NFS服务 参考韦东山的嵌入式linux应用开发完全手册 http://pan.baidu.com/s/1o79h3n0 第二.windows.linux以及开发 ...
- POJ 3258 River Hopscotch(二分查找答案)
一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...