@@ -15,6 +15,7 @@ open class UKModalController<VM: ModalVM>: UIViewController {
1515 public let model : VM
1616
1717 private var contentViewWidthConstraint : NSLayoutConstraint ?
18+ var contentViewBottomConstraint : NSLayoutConstraint ?
1819
1920 // MARK: - Subviews
2021
@@ -53,6 +54,12 @@ open class UKModalController<VM: ModalVM>: UIViewController {
5354 fatalError ( " init(coder:) has not been implemented " )
5455 }
5556
57+ // MARK: Deinitialization
58+
59+ deinit {
60+ NotificationCenter . default. removeObserver ( self )
61+ }
62+
5663 // MARK: - Lifecycle
5764
5865 open override func viewDidLoad( ) {
@@ -65,7 +72,7 @@ open class UKModalController<VM: ModalVM>: UIViewController {
6572
6673 // MARK: - Setup
6774
68- /// Sets up the modal's subviews and gesture recognizers.
75+ /// Sets up the modal's subviews, gesture recognizers and observers .
6976 open func setup( ) {
7077 self . view. addSubview ( self . overlay)
7178 self . view. addSubview ( self . contentView)
@@ -89,13 +96,45 @@ open class UKModalController<VM: ModalVM>: UIViewController {
8996 controller. handleTraitChanges ( )
9097 }
9198 }
99+
100+ NotificationCenter . default. addObserver (
101+ self ,
102+ selector: #selector( self . handleKeyboardWillShow) ,
103+ name: UIResponder . keyboardWillShowNotification,
104+ object: nil
105+ )
106+
107+ NotificationCenter . default. addObserver (
108+ self ,
109+ selector: #selector( self . handleKeyboardWillHide) ,
110+ name: UIResponder . keyboardWillHideNotification,
111+ object: nil
112+ )
92113 }
93114
94115 @objc func handleOverlayTap( ) {
95116 guard self . model. closesOnOverlayTap else { return }
96117 self . dismiss ( animated: true )
97118 }
98119
120+ @objc func handleKeyboardWillShow( notification: NSNotification ) {
121+ if let keyboardHeight = ( notification. userInfo ? [ UIResponder . keyboardFrameEndUserInfoKey] as? NSValue ) ? . cgRectValue. height {
122+ let duration = notification. userInfo ? [ UIResponder . keyboardAnimationDurationUserInfoKey] as? CGFloat ?? 0.25
123+ UIView . animate ( withDuration: duration) {
124+ self . contentViewBottomConstraint? . constant = - keyboardHeight - self . model. contentPaddings. bottom + self . view. safeAreaInsets. bottom
125+ self . view. layoutIfNeeded ( )
126+ }
127+ }
128+ }
129+
130+ @objc func handleKeyboardWillHide( notification: NSNotification ) {
131+ let duration = notification. userInfo ? [ UIResponder . keyboardAnimationDurationUserInfoKey] as? CGFloat ?? 0.25
132+ UIView . animate ( withDuration: duration) {
133+ self . contentViewBottomConstraint? . constant = - self . model. contentPaddings. bottom
134+ self . view. layoutIfNeeded ( )
135+ }
136+ }
137+
99138 // MARK: - Style
100139
101140 /// Applies styling to the modal's subviews.
0 commit comments