在刷回iOS 7时,将UIKeyboard保持为视图

时间:2014-02-21 08:23:11

标签: ios cocoa-touch ios7 core-animation uikeyboard

我有一个可以使用新的interactivePopGestureRecognizer弹出的视图控制器。如果存在键盘并且滑动动画开始,则键盘不随视图移动。我已经看过这个question并在我的视图控制器中实现了这个被解雇的

-(void)viewWillDisappear:(BOOL)animated
{ 
  [super viewWillDisappear:animated];

  [self.transitionCoordinator animateAlongsideTransitionInView:self.aTextInputView.keyboardSuperView animation:^(id<UIViewControllerTransitionCoordinatorContext> context) {

    CGRect frame = self.aTextInputView.keyboardSuperView.frame;
    frame.origin.x = self.view.frame.size.width;

    self.aTextInputView.keyboardSuperView.frame = frame;

  } completion:nil];
}

现在,当视图动画消失时我得到的是键盘从屏幕动画到x点320,这就像我设置的那样有意义,我的问题是我如何使键盘动画与向后滑?

更新

对于任何在视图消失时看到奇怪动画的人,你可以通过这样做来删除键盘。

[self.transitionCoordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context){
    if (![context isCancelled]) {
        [keyboardSuperview removeFromSuperview];
    }
}];

2 个答案:

答案 0 :(得分:3)

你的代码段中有很多自定义代码,如果我错了,请纠正我,但似乎你的self.aTextInputView.keyboardSuperView不正确。

仔细检查它不是nil。如果是,则忘记添加inputAccessoryView

以下是没有任何扩展名的完整代码段:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    UIView *keyboardSuperview = self.textField.inputAccessoryView.superview;
    [self.transitionCoordinator animateAlongsideTransitionInView:keyboardSuperview
                                                       animation:
     ^(id<UIViewControllerTransitionCoordinatorContext> context) {
         CGRect keyboardFrame = keyboardSuperview.frame;
         keyboardFrame.origin.x = self.view.bounds.size.width;
         keyboardSuperview.frame = keyboardFrame;
     }
                                                      completion:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textField.inputAccessoryView = [[UIView alloc] init];
}

答案 1 :(得分:1)

刚刚为iOS8找到了非常简单的解决方案

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.aTextInputView resignFirstResponder];
}