How to recognize keyboard frame changes when interactively turning off the keyboard?

Consider this scenario, I have a text view with a Dismiss keyboard interactively installed in the storyboard, so when the user scrolls down and can turn off the keyboard interactively. I have limitations on the text image below to make sure that it is always fully displayed in the view.

The current problem is that when the user gradually scrolls down to reject the keyboard, I cannot detect changes in the keyboard frame. I tried UIKeyboardWillHideNotification and UIKeyboardWillChangeFrameNotification , they were called only after the keyboard was fired.

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

+5
source share
3 answers

You do not have to change the height of the textView to fit all views. Instead, you should change the contentInset field so that your text object remains at the same height and you don’t have to worry about tracking the frame of the interactive keyboard. See the answer here: How to scroll a UIScrollView when a keyboard appears?

+5
source

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

Basically, you create an input-stub view for the keyboard (which constantly sticks to the keyboard, even when you drag it), and you watch the frame change. These changes are returned in the block, so you constantly use the current frame of the keyboard.

+5
source

In your viewDidLoad method, add the following line:

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

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]; } 
-2
source

Source: https://habr.com/ru/post/1234586/


All Articles