【问题标题】:UIKeyboardWillChangeFrameNotification called BEFORE textViewDidBeginEditing, but AFTER textFieldDidBeginEditing? Why?UIKeyboardWillChangeFrameNotification 在 textViewDidBeginEditing 之前调用,但在 textFieldDidBeginEditing 之后调用?为什么?
【发布时间】:2015-07-31 19:08:05
【问题描述】:

我的 ViewController 中有以下观察者。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

这个视图控制器也符合 UITextFieldDelegate 和 UITextViewDelegate 并实现了textFieldDidBeginEditingtextViewDidBeginEditing

现在,奇怪的部分来了。

如果你点击UITextField,调用顺序是textFieldDidBeginEditing AND THEN keyboardWillChangeFrame:

如果您点击UITextView,调用顺序为keyboardWillChangeFrame AND THEN 'textViewDidBeginEditing'。

没有人认为这有问题吗?不应该先调用text_____DidBeginEditing,不管是Field还是View。这是为什么呢?

这会导致奇怪的动画问题。我需要它以一种或另一种方式保持一致。

【问题讨论】:

  • 我能问你为什么使用 UIKeyboardWillChangeFrameNotification 吗?我刚做了。
  • @Everton 为视图设置动画以适应键盘的增长。我的 UITextView 或多或少占据了 90% 的视图。当键盘出现在屏幕上时,我将 UITextView 的高度设置为高于键盘的高度。当键盘消失时,我将其动画化。

标签: ios cocoa-touch uitextfield uitextview


【解决方案1】:

使用 UIKeyboardDidChangeFrameNotification 代替 UIKeyboardWillChangeFrameNotification。

【讨论】:

    【解决方案2】:

    我相信你可以使用 UIKeyboardWillShowNotification 和类似上面的代码来做你想做的事情:

    - (void)keyboardWillShowNotification:(NSNotification*)notification {
        NSDictionary *info = notification.userInfo;
    
        CGRect r = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        r = [self.view convertRect:r fromView:nil];
    
        UIViewAnimationCurve curve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        double duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:curve];
        [UIView setAnimationDuration:duration];
    
        /* view changes */
    
        [UIView commitAnimations];
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 2011-10-12
      • 2018-03-27
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2017-12-25
      • 2016-05-14
      • 2018-12-03
      相关资源
      最近更新 更多