最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用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. cocos2d-x 3.x 触摸事件

    HelloWorldScene.h bool touchBegan(cocos2d::Touch*touch, cocos2d::Event*event);//触摸开始,返回bool类型 void t ...

  2. Oracle新建用户赋只读某几张表的权限

    create user JSETI_WZQ identified by abcdef; -- 假设abcdef是密码 grant connect,resource to JSETI_WZQ; gran ...

  3. Git 分支 - 分支的衍合

    分支的衍合 把一个分支中的修改整合到另一个分支的办法有两种:merge 和 rebase(译注:rebase 的翻译暂定为“衍合”,大家知道就可以了.).在本章我们会学习什么是衍合,如何使用衍合,为什 ...

  4. 保存iptables的防火墙规则的方法【转载】

    转自: 保存iptables的防火墙规则的方法 - 51CTO.COMhttp://os.51cto.com/art/201103/249504.htm 保存iptables的防火墙规则的方法如下: ...

  5. C# Guid用法

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空 中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多 ...

  6. j2ee常用包的作用

    1.antlr-2.7.7.jar 呵呵 一句话,没有此包,hibernate不会执行hql语句   2.aopalliance-1.0.jar 这个包是AOP联盟的API包,里面包含了针对面向切面的 ...

  7. drawRect & layoutSubviews 调用时间

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘.   layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSub ...

  8. 【转】SSL/TLS/WTLS协议原理

    1 SSL(Secure Socket Layer)是netscape公司设计的主要用于web的安全传输协议.这种协议在WEB上获得了广泛的应用.2 IETF(www.ietf.org )将SSL作了 ...

  9. u-boot添加一个hello命令

    1.在common目录下建立一个cmd_hello.c文件 2.仿照/common/cmd_bootm.c文件修改,把cmd_bootm.c头文件复制过来 3.再复制do_bootm.U_BOOT_C ...

  10. go get 代理设置

    前提: 假设安装好git 我的FQ方式(也可以使用别的方式): 使用 ishadowsocks方式FQ 临时设置Windows下代理: 在控制台执行如下命令,后面的的代理值根据你具体的代理进行设置 s ...