close
The Wayback Machine - https://web.archive.org/web/20250824130807/https://github.com/ionic-team/cordova-plugin-ionic-keyboard/commit/4c03a7e0ca6821fa823128cc5e7544437932d67c
Skip to content

Commit 4c03a7e

Browse files
John Dohertyjcesarmobile
authored andcommitted
feat(ios): Added Keyboard.setKeyboardStyle method (#79)
1 parent d729cbf commit 4c03a7e

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

‎README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ cordova plugin add cordova-plugin-ionic-keyboard --save
3939
<preference name="KeyboardResizeMode" value="native" />
4040
```
4141

42+
### KeyboardStyle (for iOS only)
43+
44+
> String ('light' by default)
45+
46+
#### Possible values
47+
48+
- `light`
49+
- `dark`
50+
51+
```xml
52+
<preference name="KeyboardStyle" value="dark" />
53+
```
4254

4355
## Methods
4456

@@ -52,7 +64,7 @@ Set to true to hide the additional toolbar that is on top of the keyboard. This
5264
Keyboard.hideFormAccessoryBar(value, successCallback);
5365
```
5466

55-
##### Quick Example
67+
#### Quick Example
5668

5769
```js
5870
Keyboard.hideFormAccessoryBar(true);
@@ -70,7 +82,6 @@ Call this method to hide the keyboard
7082
Keyboard.hide();
7183
```
7284

73-
7485
### Keyboard.show
7586

7687
> Show the keyboard
@@ -81,16 +92,27 @@ Call this method to show the keyboard.
8192
Keyboard.show();
8293
```
8394

84-
### Keyboard.setResizeMode (for iOS only)


95+
### Keyboard.setResizeMode (for iOS only)
96+
8597
> Programmatically set the resize mode
8698
87-


Call the method with parameter to set the resize mode.


99+
Call the method with parameter to set the resize mode.
100+
101+
```js
102+
// Possible values are the same as for 'KeyboardResizeMode' preference
103+
Keyboard.setResizeMode('native');
104+
Keyboard.setResizeMode('body');
105+
Keyboard.setResizeMode('ionic');
106+
```
88107

89-
```js

90-
// Possible values are the same as for 'KeyboardResizeMode' preference

91-
Keyboard.setResizeMode('native');

92-
Keyboard.setResizeMode('body');

93-
Keyboard.setResizeMode('ionic');

108+
### Keyboard.setKeyboardStyle (for iOS only)
109+
110+
> Programmatically set the keyboard style
111+
112+
```js
113+
// Possible values are the same as for 'KeyboardStyle' preference
114+
Keyboard.setKeyboardStyle('light'); // <- default
115+
Keyboard.setKeyboardStyle('dark');
94116
```
95117

96118
## Properties

‎src/ios/CDVIonicKeyboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@property (readwrite, assign, nonatomic) BOOL shrinkView;
2525
@property (readwrite, assign, nonatomic) BOOL disableScrollingInShrinkView;
2626
@property (readwrite, assign, nonatomic) BOOL hideFormAccessoryBar;
27+
@property (readwrite, assign, nonatomic) NSString* keyboardStyle;
2728
@property (readonly, assign, nonatomic) BOOL keyboardIsVisible;
2829

2930
- (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command;

‎src/ios/CDVIonicKeyboard.m

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,17 @@ - (id)settingForKey:(NSString *)key
5353

5454
#pragma mark Initialize
5555

56+
NSString* UIClassString;
57+
NSString* WKClassString;
58+
NSString* UITraitsClassString;
59+
NSString* _keyboardStyle;
60+
5661
- (void)pluginInitialize
5762
{
63+
UIClassString = [@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""];
64+
WKClassString = [@[@"WK", @"Content", @"View"] componentsJoinedByString:@""];
65+
UITraitsClassString = [@[@"UI", @"Text", @"Input", @"Traits"] componentsJoinedByString:@""];
66+
5867
NSDictionary *settings = self.commandDelegate.settings;
5968

6069
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarDidChangeFrame:) name: UIApplicationDidChangeStatusBarFrameNotification object:nil];
@@ -74,10 +83,17 @@ - (void)pluginInitialize
7483
self.keyboardResizes = ResizeBody;
7584
}
7685
}
77-
NSLog(@"CDVIonicKeyboard: resize mode %d", self.keyboardResizes);
86+
NSLog(@"CDVIonicKeyboard: resize mode %lu", (unsigned long)self.keyboardResizes);
7887
}
7988
self.hideFormAccessoryBar = [settings cordovaBoolSettingForKey:@"HideKeyboardFormAccessoryBar" defaultValue:YES];
8089

90+
// get the KeyboardStyle value from config.xml
91+
NSString *keyboardStyle = [settings cordovaSettingForKey:@"KeyboardStyle"];
92+
if (keyboardStyle) {
93+
// call to setKeyboardStyle
94+
[self setKeyboardStyle:keyboardStyle];
95+
}
96+
8197
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
8298

8399
[nc addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
@@ -139,6 +155,9 @@ - (void)onKeyboardWillShow:(NSNotification *)note
139155
[self setKeyboardHeight:height delay:duration+0.2];
140156
[self resetScrollView];
141157
}
158+
159+
// call to setKeyboardStyle in case it's changed
160+
[self setKeyboardStyle:self.keyboardStyle];
142161

143162
NSString *js = [NSString stringWithFormat:@"Keyboard.fireOnShowing(%d);", (int)height];
144163
[self.commandDelegate evalJs:js];
@@ -228,6 +247,48 @@ - (void)_updateFrame
228247
[self resetScrollView];
229248
}
230249

250+
#pragma mark Keyboard Style
251+
252+
- (NSString*)keyboardStyle
253+
{
254+
return _keyboardStyle;
255+
}
256+
257+
- (void)setKeyboardStyle:(NSString*)style
258+
{
259+
IMP newImp = [style isEqualToString:@"dark"] ? imp_implementationWithBlock(^(id _s) {
260+
return UIKeyboardAppearanceDark;
261+
}) : imp_implementationWithBlock(^(id _s) {
262+
return UIKeyboardAppearanceLight;
263+
});
264+
265+
if (self.isWK) {
266+
for (NSString* classString in @[WKClassString, UITraitsClassString]) {
267+
Class c = NSClassFromString(classString);
268+
Method m = class_getInstanceMethod(c, @selector(keyboardAppearance));
269+
270+
if (m != NULL) {
271+
method_setImplementation(m, newImp);
272+
} else {
273+
class_addMethod(c, @selector(keyboardAppearance), newImp, "l@:");
274+
}
275+
}
276+
}
277+
else {
278+
for (NSString* classString in @[UIClassString, UITraitsClassString]) {
279+
Class c = NSClassFromString(classString);
280+
Method m = class_getInstanceMethod(c, @selector(keyboardAppearance));
281+
282+
if (m != NULL) {
283+
method_setImplementation(m, newImp);
284+
} else {
285+
class_addMethod(c, @selector(keyboardAppearance), newImp, "l@:");
286+
}
287+
}
288+
}
289+
290+
_keyboardStyle = style;
291+
}
231292

232293
#pragma mark HideFormAccessoryBar
233294

@@ -240,9 +301,6 @@ - (void)setHideFormAccessoryBar:(BOOL)hideFormAccessoryBar
240301
return;
241302
}
242303

243-
NSString* UIClassString = [@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""];
244-
NSString* WKClassString = [@[@"WK", @"Content", @"View"] componentsJoinedByString:@""];
245-
246304
Method UIMethod = class_getInstanceMethod(NSClassFromString(UIClassString), @selector(inputAccessoryView));
247305
Method WKMethod = class_getInstanceMethod(NSClassFromString(WKClassString), @selector(inputAccessoryView));
248306

@@ -301,6 +359,17 @@ -(void)setResizeMode:(CDVInvokedUrlCommand *)command
301359
}
302360
}
303361

362+
- (void)keyboardStyle:(CDVInvokedUrlCommand*)command
363+
{
364+
id value = [command.arguments objectAtIndex:0];
365+
if ([value isKindOfClass:[NSString class]]) {
366+
value = [(NSString*)value lowercaseString];
367+
} else {
368+
value = @"light";
369+
}
370+
371+
self.keyboardStyle = value;
372+
}
304373

305374
#pragma mark dealloc
306375

‎www/android/keyboard.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var argscheck = require('cordova/argscheck'),
33
exec = require('cordova/exec'),
44
channel = require('cordova/channel');
55

6-
76
var Keyboard = function () {};
87

98
Keyboard.fireOnShow = function (height) {
@@ -58,6 +57,10 @@ Keyboard.setResizeMode = function (mode) {
5857
console.warn("Keyboard.setResizeMode() not supported in Android");
5958
}
6059

60+
Keyboard.setKeyboardStyle = function(style) {
61+
console.warn("Keyboard.setKeyboardStyle() not supported in Android");
62+
};
63+
6164
channel.onCordovaReady.subscribe(function () {
6265
exec(success, null, 'Keyboard', 'init', []);
6366

‎www/ios/keyboard.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Keyboard.setResizeMode = function (mode) {
9494
exec(null, null, "Keyboard", "setResizeMode", [mode]);
9595
}
9696

97+
Keyboard.setKeyboardStyle = function(style) {
98+
exec(null, null, "Keyboard", "keyboardStyle", [style]);
99+
};
100+
97101
Keyboard.isVisible = false;
98102

99103
module.exports = Keyboard;

0 commit comments

Comments
 (0)