自动约束约束 – 键盘

我卡住试图animation顺利有一个自动布局的限制。 在我的.h中有一个对“keyboardHeight”的约束的引用,并把它和IB联系起来。 我想要做的就是在popup的时候用键盘对桌面视图进行animation处理。 这是我的代码:

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

事情是animation块是瞬间的,我看到空白键盘完成其animation之前出现。 所以基本上我看到了键盘animation的视图的白色背景。 只要键盘animation,我不能使animation持续。

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

试试这个方法:

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

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

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

在Spring框架中的KeyboardLayoutConstraint 。 迄今为止我find的最简单的解决scheme。 KeyboardLayoutConstraint

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

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

我采取的方法是添加一个跟随键盘大小的视图。 把它添加到你的tableview下面,或者是文本input或者其他任何东西,当键盘出现的时候它会把东西往上推。

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

 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