Skip to content

Commit df61756

Browse files
committed
Block now returns the keyboard's frame. Useful for more complicated interface changes.
1 parent eb82886 commit df61756

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

DAKeyboardControl/DAKeyboardControl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import <UIKit/UIKit.h>
1010

11-
typedef void (^DAKeyboardDidMoveBlock)(CGPoint keyboardOriginInView);
11+
typedef void (^DAKeyboardDidMoveBlock)(CGRect keyboardFrameInView);
1212

1313
@interface UIView (DAKeyboardControl)
1414

@@ -19,6 +19,6 @@ typedef void (^DAKeyboardDidMoveBlock)(CGPoint keyboardOriginInView);
1919

2020
- (void)removeKeyboardControl;
2121

22-
- (CGPoint)keyboardOriginInView;
22+
- (CGRect)keyboardFrameInView;
2323

2424
@end

DAKeyboardControl/DAKeyboardControl.m

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,21 @@ - (void)addKeyboardControl:(BOOL)panning actionHandler:(DAKeyboardDidMoveBlock)a
9393
}
9494
}
9595

96-
- (CGPoint)keyboardOriginInView
96+
- (CGRect)keyboardFrameInView
9797
{
9898
if (self.keyboardActiveView)
9999
{
100-
CGPoint keyboardWindowOrigin = self.keyboardActiveView.frame.origin;
101-
return [self convertPoint:keyboardWindowOrigin fromView:self.keyboardActiveView.window];
100+
CGRect keyboardFrameInView = [self convertRect:self.keyboardActiveView.frame
101+
fromView:self.keyboardActiveView.window];
102+
return keyboardFrameInView;
102103
}
103104
else
104105
{
105-
CGPoint keyboardWindowOrigin = CGPointMake(0.0f, [[UIScreen mainScreen] bounds].size.height);
106-
return [self convertPoint:keyboardWindowOrigin fromView:self.keyboardActiveView.window];
106+
CGRect keyboardFrameInView = CGRectMake(0.0f,
107+
[[UIScreen mainScreen] bounds].size.height,
108+
0.0f,
109+
0.0f);
110+
return keyboardFrameInView;
107111
}
108112
}
109113

@@ -188,7 +192,7 @@ - (void)keyboardWillShow:(NSNotification *)notification
188192
options:keyboardTransitionAnimationCurve
189193
animations:^{
190194
if (self.keyboardDidMoveBlock)
191-
self.keyboardDidMoveBlock(keyboardEndFrameView.origin);
195+
self.keyboardDidMoveBlock(keyboardEndFrameView);
192196
}
193197
completion:^(BOOL finished){
194198
}];
@@ -226,7 +230,7 @@ - (void)keyboardWillChangeFrame:(NSNotification *)notification
226230
options:keyboardTransitionAnimationCurve
227231
animations:^{
228232
if (self.keyboardDidMoveBlock)
229-
self.keyboardDidMoveBlock(keyboardEndFrameView.origin);
233+
self.keyboardDidMoveBlock(keyboardEndFrameView);
230234
}
231235
completion:^(BOOL finished){
232236
}];
@@ -255,7 +259,7 @@ - (void)keyboardWillHide:(NSNotification *)notification
255259
options:keyboardTransitionAnimationCurve
256260
animations:^{
257261
if (self.keyboardDidMoveBlock)
258-
self.keyboardDidMoveBlock(keyboardEndFrameView.origin);
262+
self.keyboardDidMoveBlock(keyboardEndFrameView);
259263
}
260264
completion:^(BOOL finished){
261265
}];
@@ -332,7 +336,7 @@ - (void)panGestureDidChange:(UIPanGestureRecognizer *)gesture
332336
animations:^{
333337
[self.keyboardActiveView setFrame:newKeyboardViewFrame];
334338
if (self.keyboardDidMoveBlock)
335-
self.keyboardDidMoveBlock(newKeyboardViewFrameInView.origin);
339+
self.keyboardDidMoveBlock(newKeyboardViewFrameInView);
336340
}
337341
completion:^(BOOL finished){
338342
}];
@@ -355,7 +359,7 @@ - (void)panGestureDidChange:(UIPanGestureRecognizer *)gesture
355359
animations:^{
356360
[self.keyboardActiveView setFrame:newKeyboardViewFrame];
357361
if (self.keyboardDidMoveBlock)
358-
self.keyboardDidMoveBlock(newKeyboardViewFrameInView.origin);
362+
self.keyboardDidMoveBlock(newKeyboardViewFrameInView);
359363
}
360364
completion:^(BOOL finished){
361365
if (!within44Pixels)
@@ -383,7 +387,7 @@ - (void)panGestureDidChange:(UIPanGestureRecognizer *)gesture
383387
animations:^{
384388
[self.keyboardActiveView setFrame:newKeyboardViewFrame];
385389
if (self.keyboardDidMoveBlock)
386-
self.keyboardDidMoveBlock(newKeyboardViewFrameInView.origin);
390+
self.keyboardDidMoveBlock(newKeyboardViewFrameInView);
387391
}
388392
completion:^(BOOL finished){
389393
if (!within44Pixels)

DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ - (void)viewDidLoad
5656

5757
self.view.keyboardTriggerOffset = toolBar.bounds.size.height;
5858

59-
[self.view addKeyboardPanningWithActionHandler:^(CGPoint keyboardOriginInView) {
59+
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
6060
/*
6161
Try not to call "self" inside this block (retain cycle).
6262
But if you do, make sure to remove DAKeyboardControl
@@ -65,7 +65,7 @@ - (void)viewDidLoad
6565
*/
6666

6767
CGRect toolBarFrame = toolBar.frame;
68-
toolBarFrame.origin.y = keyboardOriginInView.y - toolBarFrame.size.height;
68+
toolBarFrame.origin.y = keyboardFrameInView.origin.y - toolBarFrame.size.height;
6969
toolBar.frame = toolBarFrame;
7070

7171
CGRect tableViewFrame = tableView.frame;

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Example project included (DAKeyboardControlExample)
2222
### Adding pan-to-dismiss (functionality introduced in iMessages)
2323

2424
```objective-c
25-
[self.view addKeyboardPanningWithActionHandler:^(CGPoint keyboardOriginInView) {
25+
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
2626
// Move interface objects accordingly
2727
// Animation block is handled for you
2828
}];
@@ -31,7 +31,7 @@ Example project included (DAKeyboardControlExample)
3131
### Adding keyboard awareness (appearance and disappearance only)
3232
3333
```objective-c
34-
[self.view addKeyboardNonpanningWithActionHander:^(CGPoint keyboardOriginInView) {
34+
[self.view addKeyboardNonpanningWithActionHander:^(CGRect keyboardFrameInView) {
3535
// Move interface objects accordingly
3636
// Animation block is handled for you
3737
}];
@@ -44,7 +44,7 @@ The `keyboardTriggerOffset` property allows you to choose at what point the user
4444
```objective-c
4545
self.view.keyboardTriggerOffset = 44.0f; // Input view frame height
4646

47-
[self.view addKeyboardPanningWithActionHandler:^(CGPoint keyboardOriginInView) {
47+
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
4848
// Move input view accordingly
4949
// Animation block is handled for you
5050
}];

0 commit comments

Comments
 (0)