Ios 导航控制器按下后视野高度不正确

Ios 导航控制器按下后视野高度不正确,ios,objective-c,Ios,Objective C,我有一个集合视图,显示填充屏幕的水平单元格(它不是iOS应用程序)。某些单元格具有文本字段,如果用户点击“下一步”按钮,则集合视图将进入下一屏幕。键盘可调整包含集合视图的视图的大小。所有这些都在导航控制器中。问题在于,当键盘在屏幕上,用户点击下一步时,导航控制器(按下)的动画与键盘重叠。这使得下一个视图显示在屏幕的顶部(就像键盘仍然在那里一样) 这是调整视图大小的代码: -(void)keyboardWillShow:(NSNotification*)notification { NS

我有一个集合视图,显示填充屏幕的水平单元格(它不是iOS应用程序)。某些单元格具有文本字段,如果用户点击“下一步”按钮,则集合视图将进入下一屏幕。键盘可调整包含集合视图的视图的大小。所有这些都在导航控制器中。问题在于,当键盘在屏幕上,用户点击下一步时,导航控制器(按下)的动画与键盘重叠。这使得下一个视图显示在屏幕的顶部(就像键盘仍然在那里一样)

这是调整视图大小的代码:

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

}
-(无效)键盘将显示:(NSNotification*)通知{
NSDictionary*info=[通知用户信息];
NSValue*keyboardFrameValue=[info-objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration=[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationCurve animationCurve=[info[UIKeyboardAnimationCurveUserInfo]integerValue];
CGRect keyboardFrame=[keyboardFrameValue CGRectValue];
BOOL isPortrait=UIInterfaceOrientationSportrait([UIApplication sharedApplication].statusBarOrientation);
CGFloat keyboardHeight=isPortrait?keyboardFrame.size.height:keyboardFrame.size.width;
self.bottomConstraint.constant=-键盘高度;
[self-setNeedsUpdateConstraints];

[UIView animateWithDuration:animationDuration delay:0.0选项:(animationCurve尝试侦听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\u async在主线程上将转换异步分派到下一个屏幕

编辑:


您是在设备上还是在模拟器中进行测试?模拟器存在一些与键盘显示/隐藏相关的错误。

这可能是您问题的解决方案:

单击“下一步”按钮:

        [collectionviewCell.textField resignFirstResponder];

这与其说是一个解决方案,不如说是一个解决方案,但在显示键盘时,您能否禁用“下一步”键?我使用[self.collectionView endEditing:YES];这在设备和模拟器上都存在。如果我用你的方法代替2,情况完全相同,因为键盘将显示/hideI在几周前遇到了类似的问题。我进行了一些调试,发现有时键盘通知会被发送,尽管事实上键盘没有改变它的框架。奇怪,但上面的代码我的窍门是。如果你认为主要原因是动画重叠,请尝试使用GCD将UINavigationController推送动画推迟到下一个运行循环。但这可能是一个非常复杂的问题,如果不从代码和UI了解更多信息,我无法为你提供更多帮助。