我有一个集合视图,显示填满屏幕的水平单元格(它是一个iOS应用程序)。一些单元格有文本字段,如果用户点击" next"按钮集合视图进入下一个屏幕。键盘调整包含集合视图的视图的大小。所有这些都在导航控制器中。问题是,当键盘在屏幕上并且用户从导航控制器(推)接下来动画时,键盘消除重叠。这使得下一个视图显示在屏幕的顶部(就像键盘仍在那里)。
这是调整视图大小的代码:
-(void)keyboardWillShow:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
NSValue *keyboardFrameValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
CGFloat keyboardHeight = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;
self.bottomConstraint.constant = -keyboardHeight;
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:animationDuration delay:0.0 options:(animationCurve << 16) animations:^{
NSLog(@"keyboard will show");
[self layoutIfNeeded];
} completion:NULL];
}
-(void)keyboardWillHide:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
self.bottomConstraint.constant = 0;
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:animationDuration delay:0.0 options:(animationCurve << 16) animations:^{
NSLog(@"keyb will hide");
[self layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
[self layoutIfNeeded];
}
}];
}
有关如何解决此问题的任何想法?
答案 0 :(得分:2)
这可以解决您的问题:
单击下一步按钮:
[collectionviewCell.textField resignFirstResponder];
答案 1 :(得分:0)
尝试收听UIKeyboardWillChangeFrameNotification而不是UIKeyboardWillShowNotification和UIKeyboardWillHideNotification。 您可以通过以下方式处理UIKeyboardWillChangeFrameNotification:
- (void)handleKeyboardWillChangeFrameNotification:(NSNotification *)notification {
CGRect keyboardEndFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardEndFrame = [self.view convertRect:keyboardEndFrame fromView:nil];
UIViewAnimationCurve animationCurve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
CGFloat keyboardHeight = self.view.bounds.size.height - keyboardEndFrame.origin.y;
// Constraint animation.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
self.bottomConstraint.constant = keyboardHeight;
[self.view layoutIfNeeded];
[UIView commitAnimations];
}
如果这没有帮助,您可以尝试避免使用GCD重叠动画。 这是一个伪代码:
1)当用户点击下一个按钮时,强制当前的第一个响应者重新签名(你可以使用[self.view endEditing:YES])
2)使用dispatch_async
在主线程上异步调度到下一个屏幕的转换编辑:
您是在设备上还是在模拟器中进行测试?模拟器有一些与键盘显示/隐藏相关的错误。