danielamitay / DAKeyboardControl

DAKeyboardControl adds keyboard awareness and scrolling dismissal (ala iMessages app) to any view with only 1 line of code.
Other
1.58k stars 214 forks source link

iPad Modal - Keyboard disappearing causes message view to move below modal lower bounds and out of view #87

Closed ghost closed 9 years ago

ghost commented 9 years ago

I am trying to use this library to handle a 'message bar' (similar to Messages). It works perfectly on the iPhone, but when used in an iPad modal, dismissing the keyboard causes the message bar to animate with it and once it hits the bottom of the modal view, it moves out of view. Is there a way to ensure that the attached view respects the lower bounds of its superview when the keyboard is dismissing?

FYI - My message bar is just a UIView with a UITextView and UIButton.

ghost commented 9 years ago

Here is the fix. In a nutshell, just use a 'min()' function to determine if new frame is outside of the bounds of your containing view:

        self.view.addKeyboardNonpanningWithFrameBasedActionHandler({[unowned self] (keyboardFrameInView, opening, closing) -> Void in
            var toolbarFrame = self.messageToolbar.frame
            // Ensure message bar doesn't move outside of modal view's bounds
            toolbarFrame.origin.y = min(keyboardFrameInView.origin.y - toolbarFrame.size.height, self.view.frame.size.height - toolbarFrame.size.height)
            self.messageToolbar.frame = toolbarFrame
        }, constraintBasedActionHandler: nil)