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

Possible to create example using Storyboard and AutoLayout? #90

Open ghost opened 9 years ago

ghost commented 9 years ago

Trying to use this control in a new app that requires Storyboard/Autolayout. I see you added a 2nd block 'constraintBasedActionHandler' to each method, but I don't see any examples on how to properly use it.

I tried this but the message input view I have never moves:

        self.view.addKeyboardPanningWithFrameBasedActionHandler(nil, constraintBasedActionHandler: { [unowned self] (keyboardFrameInView, opening, closing) -> Void in
            //self.messageToolbar.bottomConstraint.constant = -keyboardFrameInView.size.height // Also tried this
            self.messageToolbar.bottomConstraint.constant = self.view.frame.size.height - keyboardFrameInView.size.height
            self.view.updateConstraintsIfNeeded()
            self.view.layoutIfNeeded()
        })

It does work when I set the frame manually and not use the auto layout constraint, but I am having to many issues with different devices and am trying to steer clear of setting frames directly.

danielamitay commented 9 years ago

I personally do not use Storyboards/AutoLayout, so if someone else would like to supply an example, that would be great!

tfalencar commented 9 years ago

Here's an example of doing this with AutoLayout:

Say you have a constraint fixing a UIView to the bottom of the screen, and we want this view to 'follow' the keyboard as we drag it down:

[self.view addKeyboardPanningWithFrameBasedActionHandler:nil 
constraintBasedActionHandler:^(CGRect keyboardFrameInView, BOOL opening, BOOL closing) 
{
        NSLog(@"constraint handler: %f", keyboardFrameInView.origin.y);
        __typeof__(self) strongSelf = weakSelf;
        if (strongSelf)
        {
            strongSelf.inputFieldBottomConstraint.constant = strongSelf.view.frame.size.height - keyboardFrameInView.origin.y;
            [strongSelf.view layoutIfNeeded];
        }
    }];

If it doesn't work its probably a good idea to test if the Autolayout constraint is correct.