本文介绍了自动布局约束 - 键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林坚持努力顺利动画表视图其中有一个自动版式约束实现的。我在.H有约束keyboardHeight的引用,并已在IB联系这件事。所有我想要做的就是用键盘动画表视图,当它弹出。这里是我的code:

Im stuck trying to animate a table view smoothly which has an autolayout contraint. I have a reference to the constraint "keyboardHeight" in my .h and have linked this up in IB. All i want to do is animate the table view with the keyboard when it pops up. Here is my code:

- (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];
    }];
}

问题是动画块被瞬间,我看到白色的空间出现在键盘完成其动画之前。所以基本上我看到的视图作为键盘的白色背景动画。我不能让动画最后,只要键盘动画。

The thing is the animation block is instantaneous and I see white space appear before the keyboard has finished its animation. So basically I see the white background of the view as the keyboard is animating. I cannot make the animation last for as long as the keyboard is animating.

我在处理这个错误的方式?在此先感谢!

Am i approaching this the wrong way? Thanks in advance!

推荐答案

尝试这种方式:

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

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

记住这个模式,因为这应该是更新基于约束的布局(根据WWDC)的正确方法。您还可以添加或删除 NSLayoutConstraint 能够像只要您拨打 setNeedsUpdateConstraints 之后。

Remember this pattern because this should be the correct way to update constraint-based layouts (according to WWDC). You can also add or remove NSLayoutConstraints as long as you call setNeedsUpdateConstraints after.

这篇关于自动布局约束 - 键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:38