【问题标题】:iOS 8 UIView not moving up when keyboard appears出现键盘时iOS 8 UIView没有向上移动
【发布时间】:2014-11-15 08:19:45
【问题描述】:

我正在开发一个聊天应用程序,其中包含 UITableViewUIView,其中包含 UITextFieldUIButton。当键盘出现时,我正在使用以下代码将UIView 向上移动。

(void)keyboardWillShow:(NSNotification *)notification
{

    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.2f animations:^{

        CGRect frame = self.inputView.frame;
       UIInterfaceOrientation orientation = self.interfaceOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation ==        UIInterfaceOrientationLandscapeRight)
            frame.origin.y -= kbSize.width;
        else
            frame.origin.y -= kbSize.height;

        self.inputView.frame = frame;
       ;
    }];
}

此代码在 iOS 7 之前运行良好,但在 iOS 8 中 UIView 未显示在键盘上方。

谁能建议可能是什么问题,或者 iOS 8 中是否有任何变化?

【问题讨论】:

    标签: uitableview uiview keyboard uitextfield ios8


    【解决方案1】:

    您的代码似乎是正确的,但我更喜欢使用 UIKeyboardDidChangeFrameNotification 或 UIKeyboardWillChangeFrameNotification,因为当键盘在视图中时,当预测文本栏上升或下降时,它们会告诉您键盘框架的变化。

    在你的 ViewDidLoad 添加这个

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardFrameDidChange:)
                                                 name:UIKeyboardDidChangeFrameNotification object:nil];
    

    然后将此方法粘贴到您的 ViewController 中

    -(void)keyboardFrameDidChange:(NSNotification*)notification{
        NSDictionary* info = [notification userInfo];
    
        CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        [yourView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y-yourView.frame.size.height, 320, yourView.frame.size.height)];
    }
    

    这将处理您的所有键盘情况,例如当它向上或向下或在其框架中使用预测文本栏进行更改时

    当你离开你的视野时也移除观察者

    【讨论】:

    • "y" 这里没有编译
    • @ngb 你到底在说什么?
    • @pankajwadhwa 在我的情况下什么都没有发生。我有一个固定在底部空间的视图。我试过你上面的代码。我正在获取 kKeyBoardFrame 的值。但是 [yourView setFrame:...] 不起作用。可能是什么原因?谢谢。
    【解决方案2】:

    接受的答案几乎是正确的。要将视图的动画与键盘的动画匹配,请使用 UIKeyboardWillChangeFrameNotification 而不是 UIKeyboardDidChangeFrameNotification。这样,您启动的动画将与键盘的动画完全匹配。这是一些代码来完成整个事情。我使用键盘的动画来驱动我的自动布局约束常量的动画,但是您可以轻松地调整它来为整个视图框架设置动画。 (注意,我们必须使用老式风格的动画来挂钩 UIKeyboardCurveInfoKey,它提供了与键盘动画完全匹配的动画曲线。

    在 viewDidLoad 中:

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

    在 ViewController 中:

    - (void)keyboardFrameDidChange:(NSNotification *)notification {
        NSDictionary *info = [notification userInfo];
    
        CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat height = kKeyBoardFrame.size.height; 
        [self.view removeConstraints:self.verticalButtonConstraints];
        NSDictionary *metrics = @{@"height" : @(height)};
        NSDictionary *views = @{@"nextButton" : self.nextButton};
    
        self.verticalButtonConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:  [nextButton(52)]-(height)-|" options:0 metrics:metrics views:views];
        [self.view addConstraints:self.verticalButtonConstraints];
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
        [UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [self.view layoutIfNeeded];
       [UIView commitAnimations];
    

    }

    【讨论】:

      【解决方案3】:

      我刚刚遇到了这个问题,并发现了一个我认为我会分享的发现。在 iOS 8 中,主视图的子视图的布局传递将在键盘即将出现或即将消失时完成。这些通道在 iOS 7 上无法完成。因此,如果您尝试在 keyBoardWillShow 或 keyboardWillChangeFrame 中为主视图的子视图设置动画,则布局通道将取消动画,并且您尝试设置动画的子视图将移回它们的原来的位置。这就是为什么 keyboardDidChangeFrame 可以为子视图设置动画而 keyboardWillChangeFrame 不能。

      我还注意到一些奇怪的事情是这些调用的时间。似乎在应用程序启动后第一次出现键盘时,对 keyboardDidChangeFrame 的调用发生得太晚了,无法使用键盘进行动画处理,因此它们一起向上滑动,但是在第二次和随后的键盘显示时,对 keyboardDidChangeFrame 的调用发生得更快,似乎您实际上可以将视图与键盘一起设置为动画。

      我必须注意,我使用 C# 和 Xamarin 作为我的 iOS 开发平台,因此在使用 Swift 或 Obj-C 时,这可能有所不同。

      【讨论】:

        【解决方案4】:

        您可以使用附件视图,它将自身附加到键盘顶部。或者,如果您想要更多的自定义功能,您可以使用@pankaj_wadwha 解释的通知来获取框架信息。奖励:您还可以获得动画信息(例如速度),以便您的视图完美地沿着键盘移动。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-18
          • 2013-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-02
          相关资源
          最近更新 更多