Skip to content

Commit 875877b

Browse files
authored
Merge pull request #324 from dogo/fix/improve-keyboard-handling
Fix keyboard covering alert buttons on iPad landscape (#300)
2 parents 870cccf + 2116242 commit 875877b

File tree

1 file changed

+63
-16
lines changed

1 file changed

+63
-16
lines changed

SCLAlertView/SCLAlertView.m

Lines changed: 63 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,71 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
670671
- (void)keyboardWillShow:(NSNotification *)notification
671672
{
672673
if(_keyboardIsVisible) return;
674+
675+
NSDictionary *userInfo = notification.userInfo;
676+
NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
677+
UIViewAnimationOptions animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
678+
679+
// Get the keyboard frame in screen coordinates
680+
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
681+
682+
// Convert contentView frame to screen coordinates
683+
UIWindow *window = self.view.window;
684+
if (!window) {
685+
window = _usingNewWindow ? _SCLAlertWindow : [SCLAlertView scl_currentKeyWindow];
686+
}
673687

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-
}];
679-
_keyboardIsVisible = YES;
688+
CGRect contentViewScreenFrame = [_contentView.superview convertRect:_contentView.frame toView:nil];
689+
CGFloat contentViewBottom = CGRectGetMaxY(contentViewScreenFrame);
690+
CGFloat keyboardTop = CGRectGetMinY(keyboardFrameEnd);
691+
692+
// Calculate how much the content view overlaps with the keyboard
693+
CGFloat overlap = contentViewBottom - keyboardTop;
694+
695+
// Add some padding so the content isn't flush against the keyboard
696+
CGFloat padding = 10.0f;
697+
698+
if (overlap > 0) {
699+
_keyboardIsVisible = YES;
700+
_tmpContentViewFrameOrigin = _contentView.frame.origin;
701+
_tmpCircleViewFrameOrigin = _circleViewBackground.frame.origin;
702+
703+
CGFloat offsetY = overlap + padding;
704+
705+
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
706+
CGRect contentFrame = self.contentView.frame;
707+
contentFrame.origin.y -= offsetY;
708+
self.contentView.frame = contentFrame;
709+
710+
CGRect circleFrame = self.circleViewBackground.frame;
711+
circleFrame.origin.y -= offsetY;
712+
self.circleViewBackground.frame = circleFrame;
713+
} completion:nil];
714+
}
680715
}
681716

682717
- (void)keyboardWillHide:(NSNotification *)notification
683718
{
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;
719+
if(_keyboardIsVisible) {
720+
NSDictionary *userInfo = notification.userInfo;
721+
NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
722+
UIViewAnimationOptions animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
723+
724+
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
725+
CGRect contentFrame = self.contentView.frame;
726+
contentFrame.origin.y = self->_tmpContentViewFrameOrigin.y;
727+
self.contentView.frame = contentFrame;
728+
729+
CGRect circleFrame = self.circleViewBackground.frame;
730+
circleFrame.origin.y = self->_tmpCircleViewFrameOrigin.y;
731+
self.circleViewBackground.frame = circleFrame;
732+
} completion:^(BOOL finished) {
733+
self->_tmpContentViewFrameOrigin = CGPointZero;
734+
self->_tmpCircleViewFrameOrigin = CGPointZero;
735+
}];
736+
737+
_keyboardIsVisible = NO;
738+
}
692739
}
693740

694741
#pragma mark - Buttons

0 commit comments

Comments
 (0)