Skip to content

Commit 15b5251

Browse files
mgefimovMaksim Efimov
and
Maksim Efimov
authored
feat(ios): keyboard avoidance (vonovak#73)
* feat: keyboard avoid * feat: keyboard avoidance gravity bottom only --------- Co-authored-by: Maksim Efimov <maksim.efimov@sbermarket.ru>
1 parent 270e53d commit 15b5251

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

ios/RNSimpleToast.mm

+3-11
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ - (void)_show:(NSString *)msg
108108
dispatch_async(dispatch_get_main_queue(), ^{
109109
RNToastViewController *controller = [RNToastViewController new];
110110
[controller show];
111-
UIView *view = [self getToastView:controller];
111+
BOOL kbdAvoidEnabled = [CSToastPositionBottom isEqualToString:positionString];
112+
UIView *view = [[RNToastView alloc] initWithFrame:controller.toastWindow.bounds kbdHeight:self->_kbdHeight kbdAvoidEnabled:kbdAvoidEnabled];
113+
[controller.toastWindow addSubview:view];
112114
UIView __weak *weakView = view;
113115

114116
UIView *toast = [view toastViewForMessage:msg title:nil image:nil style:style];
@@ -162,16 +164,6 @@ - (CGPoint)rnToast_centerPointForPosition:(NSString *)gravity withToast:(UIView
162164
return CGPointMake(view.bounds.size.width / 2.0, (view.bounds.size.height - (toast.frame.size.height / 2.0)) - bottomPadding);
163165
}
164166

165-
- (UIView *)getToastView:(RNToastViewController *)ctrl {
166-
UIView *rootView = ctrl.toastWindow;
167-
CGRect bounds = rootView.bounds;
168-
bounds.size.height -= _kbdHeight;
169-
170-
UIView *view = [[RNToastView alloc] initWithFrame:bounds];
171-
[rootView addSubview:view];
172-
return view;
173-
}
174-
175167
#ifdef RCT_NEW_ARCH_ENABLED
176168
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
177169
(const facebook::react::ObjCTurboModule::InitParams &)params {

ios/RNToastView.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
@interface RNToastView : UIView
44

5+
- (instancetype)initWithFrame:(CGRect)frame kbdHeight:(CGFloat)kbdHeight kbdAvoidEnabled:(BOOL) kbdAvoidEnabled;
6+
57
@end

ios/RNToastView.m

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
11
#import "RNToastView.h"
22

3-
@implementation RNToastView
3+
@implementation RNToastView {
4+
CGRect originalFrame;
5+
}
46

57
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
68
id hitView = [super hitTest:point withEvent:event];
79
if (hitView == self) return nil;
810
else return hitView;
911
}
1012

13+
- (instancetype)initWithFrame:(CGRect)frame kbdHeight: (CGFloat)kbdHeight kbdAvoidEnabled: (BOOL)kbdAvoidEnabled {
14+
originalFrame = frame;
15+
if (kbdAvoidEnabled) {
16+
frame.size.height -= kbdHeight;
17+
}
18+
if (self = [super initWithFrame:frame]) {
19+
if (kbdAvoidEnabled) {
20+
[[NSNotificationCenter defaultCenter] addObserver:self
21+
selector:@selector(keyboardWillShow:)
22+
name:UIKeyboardWillShowNotification
23+
object:nil];
24+
[[NSNotificationCenter defaultCenter] addObserver:self
25+
selector:@selector(keyboardWillHide:)
26+
name:UIKeyboardWillHideNotification
27+
object:nil];
28+
}
29+
}
30+
return self;
31+
}
32+
33+
- (void)keyboardWillShow:(NSNotification *)notification {
34+
CGSize keyboardSize = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
35+
CGFloat height = keyboardSize.height;
36+
CGRect frame = originalFrame;
37+
frame.size.height -= height;
38+
[self setFrame: frame];
39+
}
40+
41+
- (void)keyboardWillHide:(NSNotification *)notification {
42+
[self setFrame: originalFrame];
43+
}
44+
45+
46+
- (void)dealloc {
47+
[[NSNotificationCenter defaultCenter] removeObserver:self
48+
name:UIKeyboardWillShowNotification
49+
object:nil];
50+
[[NSNotificationCenter defaultCenter] removeObserver:self
51+
name:UIKeyboardWillHideNotification
52+
object:nil];
53+
}
54+
1155
@end

0 commit comments

Comments
 (0)