@@ -236,6 +236,7 @@ public final class TSAlertController: UIViewController {
236236 public override func viewDidLayoutSubviews( ) {
237237 super. viewDidLayoutSubviews ( )
238238
239+ initialViewTopY = view. frame. origin. y
239240 view. applyRoundCorners ( viewConfiguration. cornerRadius)
240241 }
241242
@@ -413,14 +414,12 @@ public final class TSAlertController: UIViewController {
413414
414415 // Registers notifications to handle keyboard appearance and disappearance.
415416 private func registerKeyboardNotifications( ) {
416- NotificationCenter . default. addObserver ( self ,
417- selector: #selector( keyboardWillShow ( _: ) ) ,
418- name: UIResponder . keyboardWillShowNotification,
419- object: nil )
420- NotificationCenter . default. addObserver ( self ,
421- selector: #selector( keyboardWillHide ( _: ) ) ,
422- name: UIResponder . keyboardWillHideNotification,
423- object: nil )
417+ NotificationCenter . default. addObserver (
418+ self ,
419+ selector: #selector( keyboardWillChangeFrame ( _: ) ) ,
420+ name: UIResponder . keyboardWillChangeFrameNotification,
421+ object: nil
422+ )
424423 }
425424
426425 // Registers notifications to handle keyboard appearance and disappearance.
@@ -691,7 +690,6 @@ private extension TSAlertController {
691690 }
692691 }
693692
694- ///
695693 @objc private func handleStretchyDragging( _ gesture: UIPanGestureRecognizer ) {
696694
697695 let interpolationFactor : CGFloat = 0.025
@@ -729,57 +727,45 @@ private extension TSAlertController {
729727
730728private extension TSAlertController {
731729
732- // Adjusts the alert’s position when the keyboard appears.
733- @objc func keyboardWillShow( _ notification: Notification ) {
734- // If this method is not blocked, the alert view's position may animate incorrectly
735- // when the keyboard suggestion bar appears or disappears.
736- guard !isKeyboardShown else { return }
730+ @objc func keyboardWillChangeFrame( _ notification: Notification ) {
737731
738732 guard let userInfo = notification. userInfo,
739733 let keyboardFrame = userInfo [ UIResponder . keyboardFrameEndUserInfoKey] as? CGRect ,
740- let keyboardAnimationDuration = userInfo [ UIResponder . keyboardAnimationDurationUserInfoKey] as? NSNumber else {
741- return
742- }
743-
744- let viewHeight = view. frame. height
745- let keyboardTopY = keyboardFrame. origin. y
746-
747- let duration = keyboardAnimationDuration. doubleValue
748- let adjustedViewTopY = keyboardTopY - viewConfiguration. spacing. keyboardSpacing - viewHeight
749-
750- // Move the alert up only if the spacing is smaller than the configured value.
751- // If the space between the alert and the keyboard is greater than the configured value, the alert will not move.
752- if adjustedViewTopY < initialViewTopY {
753- UIView . animate ( withDuration: duration,
754- delay: 0 ,
755- options: . curveEaseIn) {
756- self . view. frame. origin. y = adjustedViewTopY
757- }
758- }
734+ let keyboardAnimationDuration = userInfo [ UIResponder . keyboardAnimationDurationUserInfoKey] as? NSNumber
735+ else { return }
759736
760- isKeyboardShown = true
761- }
762-
763- // Resets the alert’s position when the keyboard disappears.
764- @objc func keyboardWillHide( _ notification: Notification ) {
765- // If this method is not blocked, the alert view's position may animate incorrectly
766- // when the keyboard suggestion bar appears or disappears.
767- guard isKeyboardShown else { return }
737+ let alertViewHeight = view. frame. height // Alert 뷰의 전체 높이
738+ let keyboardOriginY = keyboardFrame. origin. y // 상위 뷰 기준, 키보드의 상단 y 좌표
768739
769- guard let userInfo = notification. userInfo,
770- let keyboardAnimationDuration = userInfo [ UIResponder . keyboardAnimationDurationUserInfoKey] as? NSNumber else {
771- return
772- }
773740 let duration = keyboardAnimationDuration. doubleValue
774-
775- // Ensure the software keyboard is enabled for proper testing (Command + K).
776- UIView . animate ( withDuration: duration,
777- delay: 0 ,
778- options: . curveEaseIn) {
779- self . view. frame. origin. y = self . initialViewTopY
741+ let adjustedViewTopY = keyboardOriginY - viewConfiguration. spacing. keyboardSpacing - alertViewHeight
742+ // 키보드 상단 y 좌표에서 지정된 간격(keyboardSpacing)을 뺀 뒤,
743+ // Alert 뷰의 전체 높이를 빼면 Alert 뷰의 새로운 y 좌표를 구할 수 있음
744+
745+ // 키보드가 나타난 상태일 때 실행
746+ if isKeyboardShown {
747+ UIView . animate (
748+ withDuration: duration,
749+ delay: 0 ,
750+ options: . curveEaseIn
751+ ) {
752+ self . view. frame. origin. y = self . initialViewTopY
753+ } completion: { _ in
754+ self . isKeyboardShown = false
755+ }
756+ } else if adjustedViewTopY < initialViewTopY && !isKeyboardShown {
757+ // Move the alert up only if the spacing is smaller than the configured value.
758+ // If the space between the alert and the keyboard is greater than the configured value, the alert will not move.
759+ UIView . animate (
760+ withDuration: duration,
761+ delay: 0 ,
762+ options: . curveEaseIn
763+ ) {
764+ self . view. frame. origin. y = adjustedViewTopY
765+ } completion: { _ in
766+ self . isKeyboardShown = true
767+ }
780768 }
781-
782- isKeyboardShown = false
783769 }
784770}
785771
0 commit comments