【问题标题】:How to detect keyboard frame changes when dismiss the keyboard interactively?交互式关闭键盘时如何检测键盘框架变化?
【发布时间】:2016-01-26 13:40:42
【问题描述】:

考虑这种情况,我在情节提要中有一个带键盘 Dismiss 的文本视图,因此当用户向下滚动并能够交互地关闭键盘时。 我对底部的 textview 有限制,以确保它始终完全显示在视图上。

当前的问题是,当用户逐渐向下滚动以关闭键盘时,我无法检测到键盘框架的变化。我试过UIKeyboardWillHideNotificationUIKeyboardWillChangeFrameNotification,它们只是在键盘关闭后才被调用。

所以我的问题是,我们如何在交互式关闭键盘时同时检测键盘框架变化?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    如果您想在拖动键盘时也观察键盘框架的变化,您可以使用:https://github.com/brynbodayle/BABFrameObservingInputAccessoryView

    基本上,您为键盘创建了一个占位符输入视图(它一直粘在键盘上,即使在拖动时也是如此)并且您观察它的框架变化。这些更改会在一个块中返回,因此您始终可以获取键盘的当前帧。

    【讨论】:

    • 你也可以在scrollViewDidScroll中抓取键盘的高度,并将文本视图的底部约束更改为键盘的高度
    【解决方案2】:

    您不应更改 textView 的高度以适应所有视图。相反 - 您应该更改 contentInset 字段,以便您的 textView 将保持相同的高度,并且您不必担心交互式键盘的跟踪框架。 在这里查看答案: How do I scroll the UIScrollView when the keyboard appears?

    【讨论】:

    • 试试吧!它为我节省了很多时间:)
    • 这没有回答所提出的问题:如何跟踪交互式键盘关闭帧更改。
    【解决方案3】:

    在您的 viewDidLoad 方法中添加以下行:

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

    将这些方法添加到您的 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];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 2015-12-05
      • 1970-01-01
      • 2015-06-17
      • 2020-04-18
      • 2015-10-07
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多