|
17 | 17 | @import AVFoundation; |
18 | 18 | @import AudioToolbox; |
19 | 19 |
|
20 | | -#define KEYBOARD_HEIGHT 80 |
21 | | -#define PREDICTION_BAR_HEIGHT 40 |
22 | 20 | #define ADD_BUTTON_PADDING 10.0f |
23 | 21 | #define DEFAULT_WINDOW_WIDTH 240 |
24 | 22 |
|
@@ -58,6 +56,9 @@ @interface SCLAlertView () <UITextFieldDelegate, UIGestureRecognizerDelegate> |
58 | 56 | @property (nonatomic) CGFloat subTitleHeight; |
59 | 57 | @property (nonatomic) CGFloat subTitleY; |
60 | 58 |
|
| 59 | +@property (nonatomic) CGPoint tmpContentViewFrameOrigin; |
| 60 | +@property (nonatomic) CGPoint tmpCircleViewFrameOrigin; |
| 61 | + |
61 | 62 | @end |
62 | 63 |
|
63 | 64 | @implementation SCLAlertView |
@@ -670,25 +671,71 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField |
670 | 671 | - (void)keyboardWillShow:(NSNotification *)notification |
671 | 672 | { |
672 | 673 | 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 | + } |
673 | 687 |
|
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 | + } |
680 | 715 | } |
681 | 716 |
|
682 | 717 | - (void)keyboardWillHide:(NSNotification *)notification |
683 | 718 | { |
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 | + } |
692 | 739 | } |
693 | 740 |
|
694 | 741 | #pragma mark - Buttons |
|
0 commit comments