最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用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开发之监听键盘高度的变化的更多相关文章

  1. iOS开发之监听键盘高度的变化 分类: ios技术 2015-04-21 12:04 233人阅读 评论(0) 收藏

    最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...

  2. iOS 监听键盘高度,输入框上升

    //设置输入框 ---<因为输入框用了get方法,所以第一次调用输入框要用self 调用>: self.textlab.frame=CGRectMake(, , , ); _textlab ...

  3. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  4. 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  5. IOS开发-cell的动态高度

    tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方.现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell 首先要准备两个模型,一个是存放数据的 ...

  6. iOS开发 准确计算Coretext高度

    - (int)getAttributedStringHeightWithString:(NSAttributedString *)  string  WidthValue:(int) width{   ...

  7. iOS开发--改变tableHeaderView的高度

    1.先获取tableHeaderView 2.设置它的frame 3.将该view设置回tableview UIView *view=tableView. tableHeaderView; view. ...

  8. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

  9. iOS开发--iOS及Mac开源项目和学习资料

    文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...

随机推荐

  1. jquery 限制字数 显示输入字数 插件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Java BigDecimal Class

    Using BigDecimal to perform precise calculations with floats. BigDecimal is a class type. So declare ...

  3. db2导入表结构和表数据

    http://www.cnblogs.com/kfarvid/archive/2010/12/15/1906776.html   db2的博文 -bash-3.2$ db2 connect to ca ...

  4. HDU 3691 Nubulsa Expo

    无向图的最小割.套了个模板. #include<iostream> #include<cstdio> #include<cstring> #include<a ...

  5. Android开发教程

    http://www.cnblogs.com/liulikui/archive/2011/11/13/2247280.html 博客链接——>环境搭建

  6. python插入mysql新值

    #Server Connection to MySQL: import MySQLdb conn = MySQLdb.connect(host= "localhost", user ...

  7. docker私有仓库搭建(ubuntu 14.04和centos7)

    最近是在做一个关于docker云化的项目,马上就要开始实战.下午先做了一个私有仓库搭建的实验,先大概做个笔记,有兴趣的蛮看一下吧. 先在所有机子上都安装上docker,我的是两台ubuntu,分别是1 ...

  8. 常用Select语句

    --语 句                                功 能--数据操作SELECT      --从数据库表中检索数据行和列INSERT      --向数据库表添加新数据行DE ...

  9. SDAU课程练习--problemC

    题目描述 Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji ...

  10. 如何把一个TXT文本文件按行数分割成多个文本文件

    2011-04-27 12:00:24|  分类: 默认分类 |字号 订阅     网上有很多文本分割软件都是按字节大小来分割的,主要用于小说类的文本分割,对于比较有规则的内容按行数进行分割非常不方便 ...