iOS开发之监听键盘高度的变化 分类: ios技术 ...

最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用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;
     
     // 动画的持续时间
     double duration = [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
@interface UIView (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
@implementation UIView (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
{
     return self.frame.origin.x;
}
 
- (CGFloat)y
{
     return self.frame.origin.y;
}
 
- ( void )setCenterX:(CGFloat)centerX
{
     CGPoint center = self.center;
     center.x = centerX;
     self.center = center;
}
 
- (CGFloat)centerX
{
     return self.center.x;
}
 
- ( void )setCenterY:(CGFloat)centerY
{
     CGPoint center = self.center;
     center.y = centerY;
     self.center = center;
}
 
- (CGFloat)centerY
{
     return self.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
{
     return self.frame.size.height;
}
 
- (CGFloat)width
{
     return self.frame.size.width;
}
 
- ( void )setSize:(CGSize)size
{
     CGRect frame = self.frame;
     frame.size = size;
     self.frame = frame;
}
 
- (CGSize)size
{
     return self.frame.size;
}
 
- ( void )setOrigin:(CGPoint)origin
{
     CGRect frame = self.frame;
     frame.origin = origin;
     self.frame = frame;
}
 
- (CGPoint)origin
{
     return self.frame.origin;
}
@end
有需要的朋友可以直接用

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/liuqixu/p/4683977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值