How to detect keyboard frame changes when dismiss the keyboard interactively?

孤者浪人 提交于 2021-01-26 03:49:41

问题


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?


回答1:


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?




回答2:


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.




回答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];


}


来源:https://stackoverflow.com/questions/33362987/how-to-detect-keyboard-frame-changes-when-dismiss-the-keyboard-interactively

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!