Skip to content

Commit

Permalink
Improve keyboard handling #300
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Apr 6, 2022
1 parent 09245fd commit fc7cec8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
56 changes: 40 additions & 16 deletions SCLAlertView/SCLAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#import <AudioToolbox/AudioToolbox.h>
#endif

#define KEYBOARD_HEIGHT 80
#define PREDICTION_BAR_HEIGHT 40
#define ADD_BUTTON_PADDING 10.0f
#define DEFAULT_WINDOW_WIDTH 240

Expand Down Expand Up @@ -62,6 +60,9 @@ @interface SCLAlertView () <UITextFieldDelegate, UIGestureRecognizerDelegate>
@property (nonatomic) CGFloat subTitleHeight;
@property (nonatomic) CGFloat subTitleY;

@property (nonatomic) CGPoint tmpContentViewFrameOrigin;
@property (nonatomic) CGPoint tmpCircleViewFrameOrigin;

@end

@implementation SCLAlertView
Expand Down Expand Up @@ -636,25 +637,48 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
- (void)keyboardWillShow:(NSNotification *)notification
{
if(_keyboardIsVisible) return;

[UIView animateWithDuration:0.2f animations:^{
CGRect f = self.view.frame;
f.origin.y -= KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
self.view.frame = f;
}];

NSDictionary *userInfo = [notification userInfo];
CGRect endKeyBoardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat endKeyBoardFrame = CGRectGetMinY(endKeyBoardRect);

_keyboardIsVisible = YES;

_tmpContentViewFrameOrigin = _contentView.frame.origin;
_tmpCircleViewFrameOrigin = _circleViewBackground.frame.origin;

CGFloat newContentViewFrameY = CGRectGetMaxY(_contentView.frame) - endKeyBoardFrame;

if(!IS_LANDSCAPE && newContentViewFrameY < 0) {
newContentViewFrameY = 0;
}

CGFloat newBallViewFrameY = _circleViewBackground.frame.origin.y - fabs(newContentViewFrameY);

CGRect contentFrame = self.contentView.frame;
contentFrame.origin.y -= fabs(newContentViewFrameY);
self.contentView.frame = contentFrame;

CGRect circleFrame = self.circleViewBackground.frame;
circleFrame.origin.y = newBallViewFrameY;
self.circleViewBackground.frame = circleFrame;
}

- (void)keyboardWillHide:(NSNotification *)notification
{
if(!_keyboardIsVisible) return;

[UIView animateWithDuration:0.2f animations:^{
CGRect f = self.view.frame;
f.origin.y += KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
self.view.frame = f;
}];
_keyboardIsVisible = NO;
if(_keyboardIsVisible) {
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.y = _tmpContentViewFrameOrigin.y;
self.contentView.frame = contentFrame;
_tmpContentViewFrameOrigin = CGPointZero;

CGRect circleFrame = self.circleViewBackground.frame;
circleFrame.origin.y = _tmpCircleViewFrameOrigin.y;
self.circleViewBackground.frame = circleFrame;
_tmpCircleViewFrameOrigin = CGPointZero;

_keyboardIsVisible = NO;
}
}

#pragma mark - Buttons
Expand Down
2 changes: 2 additions & 0 deletions SCLAlertView/SCLMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define IS_LANDSCAPE UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])

#endif

0 comments on commit fc7cec8

Please sign in to comment.