iOS8:在键盘转换期间移动视图会发生什么?

Pit*_*rou 8 animation uikeyboard ios8

切换到iOS8后,当我在键盘转换期间移动视图时,我会遇到奇怪的行为.谁能解释一下发生了什么?

这是一个演示问题的最小例子.我有一个简单的视图与a UITextField和a UIButton.该功能nudgeUp将文本字段和按钮向上移动10个点.它由buttonPressed回调或keyboardWillShow回调触发.

当我点击按钮时,代码按预期工作:buttonPressed调用nudgeUp和按钮和文本字段跳起10点.

当我点击文本字段,keyboardWillShow调用nudgeUp,但行为是非常不同的.按钮和文本字段立即向下跳过10个点,然后在键盘显示时自动向后滑动到原始位置.

为什么会这样?如何在iOS8键盘演示期间重新获得对动画的控制?

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

}

- (void)keyboardWillShow:(NSNotification *)notification
{
    // Called when the keyboard appears.
    [self nudgeUp];
}

- (IBAction)buttonPressed:(id)sender {
    [self nudgeUp];
}

- (void)nudgeUp
{
    CGRect newTextFieldFrame = self.textField.frame;
    newTextFieldFrame.origin.y -= 10;
    self.textField.frame = newTextFieldFrame;

    CGRect newButtonFrame = self.button.frame;
    newButtonFrame.origin.y -= 10;
    self.button.frame = newButtonFrame;
}
@end
Run Code Online (Sandbox Code Playgroud)

小智 8

这是AutoLayout.在iOS8中有些变化,如果你启用了AutoLayout,你就不能再改变帧或中心点了.您必须创建约束的出口(垂直空间)并相应地更新它,而不是更改框架位置.约束就像任何其他ui控件一样,可以有一个插座.约束更改可以设置动画.

例:

[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{        
    self.bottomSpaceConstraint.constant = adjustmentedValue;
    [self.view layoutIfNeeded];        
} completion:^(BOOL finished) {
}];
Run Code Online (Sandbox Code Playgroud)