Skip to content

Commit cf8a011

Browse files
committed
Improve keyboard handling #300
1 parent a175d5c commit cf8a011

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

SCLAlertView/SCLAlertView.m

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
@import AVFoundation;
1818
@import AudioToolbox;
1919

20-
#define KEYBOARD_HEIGHT 80
21-
#define PREDICTION_BAR_HEIGHT 40
2220
#define ADD_BUTTON_PADDING 10.0f
2321
#define DEFAULT_WINDOW_WIDTH 240
2422

@@ -58,6 +56,9 @@ @interface SCLAlertView () <UITextFieldDelegate, UIGestureRecognizerDelegate>
5856
@property (nonatomic) CGFloat subTitleHeight;
5957
@property (nonatomic) CGFloat subTitleY;
6058

59+
@property (nonatomic) CGPoint tmpContentViewFrameOrigin;
60+
@property (nonatomic) CGPoint tmpCircleViewFrameOrigin;
61+
6162
@end
6263

6364
@implementation SCLAlertView
@@ -670,25 +671,48 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
670671
- (void)keyboardWillShow:(NSNotification *)notification
671672
{
672673
if(_keyboardIsVisible) return;
673-
674-
[UIView animateWithDuration:0.2f animations:^{
675-
CGRect f = self.view.frame;
676-
f.origin.y -= KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
677-
self.view.frame = f;
678-
}];
674+
675+
NSDictionary *userInfo = [notification userInfo];
676+
CGRect endKeyBoardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
677+
CGFloat endKeyBoardFrame = CGRectGetMinY(endKeyBoardRect);
678+
679679
_keyboardIsVisible = YES;
680+
681+
_tmpContentViewFrameOrigin = _contentView.frame.origin;
682+
_tmpCircleViewFrameOrigin = _circleViewBackground.frame.origin;
683+
684+
CGFloat newContentViewFrameY = CGRectGetMaxY(_contentView.frame) - endKeyBoardFrame;
685+
686+
if(!IS_LANDSCAPE && newContentViewFrameY < 0) {
687+
newContentViewFrameY = 0;
688+
}
689+
690+
CGFloat newBallViewFrameY = _circleViewBackground.frame.origin.y - fabs(newContentViewFrameY);
691+
692+
CGRect contentFrame = self.contentView.frame;
693+
contentFrame.origin.y -= fabs(newContentViewFrameY);
694+
self.contentView.frame = contentFrame;
695+
696+
CGRect circleFrame = self.circleViewBackground.frame;
697+
circleFrame.origin.y = newBallViewFrameY;
698+
self.circleViewBackground.frame = circleFrame;
680699
}
681700

682701
- (void)keyboardWillHide:(NSNotification *)notification
683702
{
684-
if(!_keyboardIsVisible) return;
685-
686-
[UIView animateWithDuration:0.2f animations:^{
687-
CGRect f = self.view.frame;
688-
f.origin.y += KEYBOARD_HEIGHT + PREDICTION_BAR_HEIGHT;
689-
self.view.frame = f;
690-
}];
691-
_keyboardIsVisible = NO;
703+
if(_keyboardIsVisible) {
704+
CGRect contentFrame = self.contentView.frame;
705+
contentFrame.origin.y = _tmpContentViewFrameOrigin.y;
706+
self.contentView.frame = contentFrame;
707+
_tmpContentViewFrameOrigin = CGPointZero;
708+
709+
CGRect circleFrame = self.circleViewBackground.frame;
710+
circleFrame.origin.y = _tmpCircleViewFrameOrigin.y;
711+
self.circleViewBackground.frame = circleFrame;
712+
_tmpCircleViewFrameOrigin = CGPointZero;
713+
714+
_keyboardIsVisible = NO;
715+
}
692716
}
693717

694718
#pragma mark - Buttons

SCLAlertView/SCLMacros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
3131
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
3232
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
3333

34+
#define IS_LANDSCAPE UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
35+
3436
#endif

0 commit comments

Comments
 (0)