Skip to content

Commit 90cf529

Browse files
author
罗瑞东
committed
fix(toast) : position error when keyboard show in iOS
1 parent 8d95162 commit 90cf529

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

ios/LRDRCTSimpleToast/LRDRCTSimpleToast.m

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,39 @@
1919
@interface LRDRCTSimpleToast : NSObject <RCTBridgeModule>
2020
@end
2121

22-
@implementation LRDRCTSimpleToast
22+
@implementation LRDRCTSimpleToast {
23+
CGFloat _keyOffset;
24+
}
25+
26+
- (instancetype)init {
27+
if (self = [super init]) {
28+
_keyOffset = 0;
29+
[[NSNotificationCenter defaultCenter] addObserver:self
30+
selector:@selector(keyboardWasShown:)
31+
name:UIKeyboardDidShowNotification
32+
object:nil];
33+
[[NSNotificationCenter defaultCenter] addObserver:self
34+
selector:@selector(keyboardWillHiden:)
35+
name:UIKeyboardWillHideNotification
36+
object:nil];
37+
}
38+
return self;
39+
}
40+
41+
- (void)keyboardWasShown:(NSNotification *)notification {
42+
43+
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
44+
45+
int height = MIN(keyboardSize.height,keyboardSize.width);
46+
int width = MAX(keyboardSize.height,keyboardSize.width);
47+
48+
_keyOffset = height;
49+
}
50+
51+
- (void)keyboardWillHiden:(NSNotification *)notification {
52+
_keyOffset = 0;
53+
}
54+
2355

2456
RCT_EXPORT_MODULE()
2557

@@ -45,6 +77,7 @@ - (void)_show:(NSString *)msg duration:(NSTimeInterval)duration gravity:(NSInteg
4577
dispatch_async(dispatch_get_main_queue(), ^{
4678
UIView *root = [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view];
4779
CGRect bound = root.bounds;
80+
bound.size.height -= _keyOffset;
4881
if (bound.size.height > LRDRCTSimpleToastBottomOffset*2) {
4982
bound.origin.y += LRDRCTSimpleToastBottomOffset;
5083
bound.size.height -= LRDRCTSimpleToastBottomOffset*2;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-simple-toast",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Simple Toast for react-native. In Android it's just native toast, in iOS it's https://github.com/scalessec/Toast",
55
"main": "index.js",
66
"scripts": {

sample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"dependencies": {
99
"react": "15.3.0",
1010
"react-native": "^0.34.1",
11-
"react-native-simple-toast": "0.0.4"
11+
"react-native-simple-toast": "0.0.5"
1212
}
1313
}

0 commit comments

Comments
 (0)