Skip to content
This repository was archived by the owner on Apr 2, 2018. It is now read-only.

Commit fa66f00

Browse files
committed
only emit js events on keyboard hide/show, set isVisible and remove the accessory bar
1 parent 70479e0 commit fa66f00

File tree

2 files changed

+29
-89
lines changed

2 files changed

+29
-89
lines changed

src/ios/IonicKeyboard.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
@interface IonicKeyboard : CDVPlugin {
44
@protected
5-
id _resizeViewShowObserver, _resizeViewHideObserver;
5+
id _keyboardShowObserver, _keyboardHideObserver;
66
}
77

8-
@property (readwrite, assign, getter = resizeView, setter = setResizeView:) CGFloat resizeView;
98
@property (readwrite, assign, getter = hideKeyboardAccessoryBar, setter = setHideKeyboardAccessoryBar:) BOOL hideKeyboardAccessoryBar;
9+
@property (readwrite, assign) BOOL keyboardIsVisible;
1010

11-
- (void) resizeView:(CDVInvokedUrlCommand*)command;
1211
- (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command;
1312
- (void) close:(CDVInvokedUrlCommand*)command;
14-
- (void) disableScroll:(CDVInvokedUrlCommand*)command;
1513

1614
@end
1715

src/ios/IonicKeyboard.m

Lines changed: 27 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,36 @@
66
@implementation IonicKeyboard
77

88
@synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
9-
@synthesize resizeView = _resizeView;
109

1110
- (void)pluginInitialize
1211
{
13-
self.resizeView = -1.0;
12+
13+
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
14+
__weak IonicKeyboard* weakSelf = self;
15+
16+
weakSelf.webView.scrollView.scrollEnabled = NO;
17+
18+
_keyboardShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
19+
object:nil
20+
queue:[NSOperationQueue mainQueue]
21+
usingBlock:^(NSNotification* notification) {
22+
23+
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
24+
keyboardFrame = [self.viewController.view convertRect:keyboardFrame fromView:nil];
25+
26+
[weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"Keyboard.isVisible = true; cordova.fireWindowEvent('ionic.showkeyboard', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
27+
28+
}];
29+
30+
_keyboardHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
31+
object:nil
32+
queue:[NSOperationQueue mainQueue]
33+
usingBlock:^(NSNotification* notification) {
34+
[weakSelf.commandDelegate evalJs:@"Keyboard.isVisible = false; cordova.fireWindowEvent('ionic.hidekeyboard'); "];
35+
}];
1436
}
1537

38+
1639
- (BOOL)hideKeyboardAccessoryBar
1740
{
1841
return _hideKeyboardAccessoryBar;
@@ -37,93 +60,16 @@ - (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar
3760

3861
/* ------------------------------------------------------------- */
3962

40-
- (CGFloat)resizeView
41-
{
42-
return _resizeView;
43-
}
44-
45-
- (void)setResizeView:(CGFloat)resizeOffset
46-
{
47-
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
48-
__weak IonicKeyboard* weakSelf = self;
49-
50-
if (resizeOffset == _resizeView) {
51-
return;
52-
}
53-
54-
if (resizeOffset >= 0) {
55-
[nc removeObserver:_resizeViewShowObserver];
56-
_resizeViewShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
57-
object:nil
58-
queue:[NSOperationQueue mainQueue]
59-
usingBlock:^(NSNotification* notification) {
60-
[weakSelf performSelector:@selector(resizeViewWillShow:) withObject:notification afterDelay:0];
61-
}];
62-
63-
[nc removeObserver:_resizeViewHideObserver];
64-
_resizeViewHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
65-
object:nil
66-
queue:[NSOperationQueue mainQueue]
67-
usingBlock:^(NSNotification* notification) {
68-
[weakSelf performSelector:@selector(resizeViewWillHide:) withObject:notification];
69-
}];
70-
}
71-
72-
_resizeView = resizeOffset;
73-
}
74-
75-
/* ------------------------------------------------------------- */
76-
77-
- (void)resizeViewWillShow:(NSNotification*)notif
78-
{
79-
if (_resizeView < 0) {
80-
return;
81-
}
82-
83-
CGPoint bottomOffset;
84-
self.webView.scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, self.resizeView, 0.0);
85-
bottomOffset = CGPointMake(0.0, self.resizeView);
86-
87-
self.webView.backgroundColor = [UIColor whiteColor];
88-
89-
[self.webView.scrollView setContentOffset:bottomOffset];
90-
91-
}
92-
93-
- (void)resizeViewWillHide:(NSNotification*)notif
94-
{
95-
self.webView.scrollView.contentInset = UIEdgeInsetsZero;
96-
}
97-
98-
/* ------------------------------------------------------------- */
99-
10063
- (void)dealloc
10164
{
102-
// since this is ARC, remove observers only
103-
104-
//Left in because I'm a noob at Obj-C Memory management, not sure if
105-
// this needs to be implemented...
106-
10765
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
10866

10967
[nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
110-
//[nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
68+
[nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
11169
}
11270

11371
/* ------------------------------------------------------------- */
11472

115-
#pragma Plugin interface
116-
117-
- (void) resizeView:(CDVInvokedUrlCommand*)command
118-
{
119-
id value = [command.arguments objectAtIndex:0];
120-
if (!([value isKindOfClass:[NSNumber class]])) {
121-
value = [NSNumber numberWithFloat:-1.0];
122-
}
123-
124-
self.resizeView = [value floatValue];
125-
}
126-
12773
- (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command
12874
{
12975
id value = [command.arguments objectAtIndex:0];
@@ -139,11 +85,7 @@ - (void) close:(CDVInvokedUrlCommand*)command
13985
[self.webView endEditing:YES];
14086
}
14187

142-
- (void) disableScroll:(CDVInvokedUrlCommand*)command
143-
{
144-
self.webView.scrollView.scrollEnabled = NO;
145-
}
146-
14788

14889
@end
14990

91+

0 commit comments

Comments
 (0)