11

Consider this scenario, I have a textview with keyboard Dismiss interactively set in storyboard, so when user scroll down and able to dismiss keyboard interactively. I have constraints on the textview to bottom to make sure it is always fully displayed on the view.

Current problem is, when user gradually scroll down to dismiss the keyboard, I can not detect the keyboard frame changes. I tried UIKeyboardWillHideNotification and UIKeyboardWillChangeFrameNotification, they were only called after the keyboard dismissed.

So my question is, how can we detect keyboard frame changes simultaneously when dismiss the keyboard interactively?

MMiroslav
  • 1,462
  • 18
  • 31
air_bob
  • 1,307
  • 12
  • 26

3 Answers3

7

If you want to observe keyboard frame changes even when the keyboard is being dragged you can use this: https://github.com/brynbodayle/BABFrameObservingInputAccessoryView

Basically you create a placeholder input view for keyboard (which sticks to the keyboard all the time, even while dragging) and you observe it's frame changes. Those changes are being returned in a block, so you get current frame of the keyboard all the time.

haluzak
  • 1,113
  • 3
  • 16
  • 30
  • You can also grab the height of the keyboard in `scrollViewDidScroll` and change the bottom constraint on the text view to be the height of the keyboard – Jay Strawn Feb 14 '19 at 18:03
4

You shouldn't change the textView height to fit all view. Instead - you should change contentInset field so your textView will stay the same height and you won't have to bother about tracking frame of the interactive keyboard. See answer here: How do I scroll the UIScrollView when the keyboard appears?

Community
  • 1
  • 1
quarezz
  • 434
  • 6
  • 11
-3

In your viewDidLoad method add these line:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillShow:)
                                         name:UIKeyboardWillShowNotification
                                       object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

the add these methods to your viewController

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.30
                          delay:0.0
                        options:(7 << 16) // This curve ignores durations
                     animations:^{
                         self.buttonsBottomConstraint.constant = keyboardSize.height - self.footerView.bounds.size.height + 4.0;
                         [self.view layoutIfNeeded];
                     }
                     completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.30
                          delay:0.0
                        options:(7 << 16) // This curve ignores durations
                     animations:^{
                         self.buttonsBottomConstraint.constant = 0.0;
                         [self.view layoutIfNeeded];

                     }
                     completion:nil];


}
Tekaya Marouene
  • 600
  • 2
  • 14