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

下面是我实现的方法:(利用通知)

1
2
3
4
5
6
7
8
9
10
11
12
//
键盘通知
    //
键盘的frame发生改变时发出的通知(位置和尺寸)
    //   
UIKeyboardWillChangeFrameNotification
    //   
UIKeyboardDidChangeFrameNotification
    //
键盘显示时发出的通知
    //   
UIKeyboardWillShowNotification
    //   
UIKeyboardDidShowNotification
    //
键盘隐藏时发出的通知
    //   
UIKeyboardWillHideNotification
    //   
UIKeyboardDidHideNotification
     
    [[NSNotificationCenter
defaultCenter] addObserver:self selector:
@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//在这里注册通知

下面是监听通知:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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;
//这里的<span
style="background-color: rgb(240, 240, 240);">self.toolbar就是我的输入框。</span>
 
        }else{
            self.toolbar.y
= keyboardF.origin.y - self.toolbar.height;
        }
    }];
}

当然,这里我为UIView写了一个类别,实现如下:

.h文件中声明

1
2
3
4
5
6
7
8
9
10
@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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@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技术 2015-04-21 12:04 233人阅读 评论(0) 收藏的更多相关文章

  1. iOS开发之监听键盘高度的变化

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

  2. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  3. 架构师速成6.7-设计开发思路-uml 分类: 架构师速成 2015-07-29 18:25 157人阅读 评论(0) 收藏

    uml是什么东西?统一建模语言,一门语言,是用来进行软件设计的一门语言. 其实一门语言的诞生并不伟大,让大多数人都使用才足够伟大.uml就是一门伟大的语言,因为目前软件设计的唯一语言就是它. UML其 ...

  4. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  5. iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏

    关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...

  6. iOS开发~CocoaPods使用详细说明 分类: ios相关 2015-04-01 16:45 68人阅读 评论(0) 收藏

    iOS开发-CocoaPods使用详细说明 一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来 ...

  7. 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏

    目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...

  8. IOS之富文本编辑 分类: ios技术 2015-03-06 22:51 89人阅读 评论(0) 收藏

    之前做项目时遇到一个问题:          使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...

  9. 浅谈IOS8之size class 分类: ios技术 2015-02-05 19:06 62人阅读 评论(0) 收藏

    文章目录 1. 简介 2. 实验 3. 实战 3.1. 修改 Constraints 3.2. 安装和卸载 Constraints 3.3. 安装和卸载 View 3.4. 其他 4. 后话 以前和安 ...

随机推荐

  1. 正确使用#include和前置声明(forward declaration)

    http://blog.csdn.net/SpriteLW/article/details/965702

  2. HDU4325--Flowers--树状数组,离散化

    Description As is known to all, the blooming time and duration varies between different kinds of flo ...

  3. CodeForces 383D Antimatter

    线性DP. dp[i][j]表示以第i个数字为结尾的,字串和为j的有几种. #include<cstdio> #include<cstring> #include<cma ...

  4. CodeForces 190D Non-Secret Cypher

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  5. C++中运行外部程序

    关于三个SDK函数: WinExec, ShellExecute,CreateProcess 的其他注意事项: [1]定义头文件 必须定义以下两个头文件: #include <shlobj.h& ...

  6. C# 通过接口 post 请求

    /// <summary> /// 提交数据请求 方法一 /// </summary> /// <param name="POSTURL">请求 ...

  7. CF 313 DIV2 B 树状数组

    http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数 ...

  8. Excel相关问题

    Excel默认永远使用最后安装的那个Excel版本打开.但是如果有一个Excel已经启动了,则使用那个Excel打开. 1.打开“开发工具”选项卡2007中:[Excel选项]-[常用]2010中:[ ...

  9. Android sdk content loader 0%的解决方案

    Eclipse在启动时,经常会碰到半天启动不起来的情况,罪魁祸首就是“Android sdk content loader 0%”,题主经常是受这玩意的百般折磨,大早上一来就被这扫了工作的激情,浪费了 ...

  10. java删除文件夹 Java中实现复制文件或文件夹

    删除文件夹 import java.io.File; public class DeleteDir { /** * @param args */ public static void main(Str ...