From 3af6966531b818617cb89422db36bbf5d4898179 Mon Sep 17 00:00:00 2001
From: Diogo Autilio <diautilio@gmail.com>
Date: Tue, 14 Sep 2021 01:01:38 -0300
Subject: [PATCH] Improve keyboard handling #300

---
 SCLAlertView/SCLAlertView.m | 56 ++++++++++++++++++++++++++-----------
 SCLAlertView/SCLMacros.h    |  2 ++
 2 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/SCLAlertView/SCLAlertView.m b/SCLAlertView/SCLAlertView.m
index 20a22dc..e71bd52 100755
--- a/SCLAlertView/SCLAlertView.m
+++ b/SCLAlertView/SCLAlertView.m
@@ -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
 
@@ -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
@@ -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
diff --git a/SCLAlertView/SCLMacros.h b/SCLAlertView/SCLMacros.h
index 1fec837..ee61d5d 100644
--- a/SCLAlertView/SCLMacros.h
+++ b/SCLAlertView/SCLMacros.h
@@ -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