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 ...
随机推荐
- linux command----vi
vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多增强功能. ...
- cell reuse & disposebag
For my project I've made base cell class TableViewCell: UITableViewCell { private(set) var disposeBa ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- CentOS安装VirtualBox增强工具
安装过程中出现错误: Bulding the VirtualBox Guest Additions Kernel modules failedYour system does not seem to ...
- HDU 3696 Farm Game
SPFA最长路,思路如下: 先对题目中给出的每条边建边,权值为转化率:增加一个终点S,每个节点到S建边,权值为该物品的单价. 假设X物品最终转化为了Y物品,那么转化之后得到的钱就是 W[x]*转化率1 ...
- ZooKeeper概述
1.Zookeeper概述 Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoop 的分布式协调服务.它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置 ...
- Centos修改默认网卡名
安装系统后默认的网卡名称为 enpXX ,修改为熟悉的eth0 1 vi /etc/default/grub GRUB_TIMEOUT=5GRUB_DEFAULT=savedGRUB_DISABLE_ ...
- SecureCRT 上传文件的两种方法 Zmodem、SFTP
Zmodem: 无论有xshell还是secureCRT连接linux的时. 默认都用一个zmodem可以帮助window和linux之间传输文件 很方便和实用的工具. 不过默认是无法使用的 需要安装 ...
- emacs format
格式化源码是很常见的需求,emacs有个indent-region函数用于格式化选定的代码,前提是你处在某个非text mode下,如c-mode或者java-mode之类.如果要格式化整个文件,你需 ...
- C#入门经典(第三章-1)
#region--------#endregion 此关键字 将设置代码是否可以折叠和展开.但是他们不是C#关键字.注意:带#的是预处理指令,严格说不是C#关键字. 变量: