35

我坚持尝试平滑地为具有自动布局约束的表格视图设置动画。我在我的 .h 中引用了约束“keyboardHeight”,并在 IB 中将其链接起来。我要做的就是在弹出时使用键盘为表格视图设置动画。这是我的代码:

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];
    CGFloat height = keyboardFrame.size.height;

    [UIView animateWithDuration:animationDuration animations:^{
        self.keyboardHeight.constant = -height;
        [self.view setNeedsLayout];
    }];
}

问题是动画块是瞬时的,我看到在键盘完成动画之前出现空白。所以基本上我在键盘动画时看到视图的白色背景。只要键盘正在动画,我就无法使动画持续。

我是以错误的方式接近这个吗?提前致谢!

4

4 回答 4

52

试试这种方式:

self.keyboardHeight.constant = -height;
[self.view setNeedsUpdateConstraints];

[UIView animateWithDuration:animationDuration animations:^{
   [self.view layoutIfNeeded];
}];

记住这个模式,因为这应该是更新基于约束的布局的正确方法(根据 WWDC)。你也可以添加或删除NSLayoutConstraints,只要你调用setNeedsUpdateConstraints之后。

于 2012-10-17T00:58:02.167 回答
15

如果您使用的是 UITableViewController,则 iOS 应自动适应键盘大小以调整 contentInsets。但是如果你的 tableView 在 UIViewController 里面,你可能想用这个:

Spring框架中的KeyboardLayoutConstraint 。到目前为止我找到的最简单的解决方案。 键盘布局约束

于 2015-02-01T17:08:10.920 回答
8

尝试下一个代码。在这种情况下,表格视图布局在屏幕的底部边缘。

- (void)keyboardWillShow:(NSNotification *)notification { // UIKeyboardWillShowNotification

    NSDictionary *info = [notification userInfo];
    NSValue *keyboardFrameValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [keyboardFrameValue CGRectValue];

    BOOL isPortrait = UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
    CGFloat keyboardHeight = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;

    // constrBottom is a constraint defining distance between bottom edge of tableView and bottom edge of its superview
    constrBottom.constant = keyboardHeight; 
    // or constrBottom.constant = -keyboardHeight - in case if you create constrBottom in code (NSLayoutConstraint constraintWithItem:...:toItem:...) and set views in inverted order

    [UIView animateWithDuration:animationDuration animations:^{
        [tableView layoutIfNeeded];
    }];
}


- (void)keyboardWillHide:(NSNotification *)notification { // UIKeyboardWillHideNotification

    NSDictionary *info = [notification userInfo];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    constrBottom.constant = 0;
    [UIView animateWithDuration:animationDuration animations:^{
        [tableView layoutIfNeeded];
    }];
}
于 2013-07-11T16:05:34.100 回答
2

我采用的方法是添加一个遵循键盘大小的视图。将它添加到您的表格视图或文本输入或其他任何内容下方,当键盘出现时它会向上推。

这就是我设置视图层次结构的方式:

NSDictionary *views = @{@"chats": self.chatsListView, @"reply": self.replyBarView, @"fakeKeyboard":self.fakeKeyboardView};
[self.view addVisualConstraints:@"V:|-30-[chats][reply][fakeKeyboard]|" views:views];

然后键盘尺寸跟随视图的关键位如下所示:

- (void)keyboardWillShow:(NSNotification *)notification
{
    // Save the height of keyboard and animation duration
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.desiredHeight = CGRectGetHeight(keyboardRect);
    self.duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [self animateSizeChange];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    self.desiredHeight = 0.0f;

    [self animateSizeChange];
}

- (CGSize)intrinsicContentSize
{
    return CGSizeMake(UIViewNoIntrinsicMetric, self.desiredHeight);
}

- (void)animateSizeChange
{
    [self invalidateIntrinsicContentSize];

    // Animate transition
    [UIView animateWithDuration:self.duration animations:^{
        [self.superview layoutIfNeeded];
    }];
}

让这个特定视图处理其大小调整的好处是,您可以让视图控制器忽略它,并且您还可以在您的应用程序中任何您想要将所有内容向上移动的地方重新使用此视图。

完整文件在这里: https ://gist.github.com/shepting/6025439

于 2013-07-18T18:36:56.547 回答