显示键盘时导航栏不可见

Nav Bar invisible when keyboard is displayed

textView 显示或隐藏键盘时,我使用下面的代码向上或向下移动屏幕,这样我可以在聚焦时看到最后一个字段。

- (void)textViewDidBeginEditing:(UITextView *)textField
{
    [self animateTextView: textField up: YES];
}


- (void)textViewDidEndEditing:(UITextView *)textField
{
    [self animateTextView: textField up: NO];
}

- (void) animateTextView: (UITextView*) textField up: (BOOL) up
{
    const int movementDistance = 130; // tweak as needed
    const float movementDuration = 0.2f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

但是问题是navBar在屏幕上移的时候也跟着移动了,有没有办法把除了navBar以外的都移动,或者把navBar留在原来的位置使用 [self animateTextView: textField up: YES];?

那是因为您的主视图中有自定义 NavigationBar。 在情节提要中的 NavBar 下面创建另一个 UIView,然后将 textField 和其他 UI 对象放入这个新视图中。 记住这一点,注意 navBar 不应该位于出现键盘时向上推的同一个 UIView 对象。 因此,创建这个新创建的 UIView 对象的出口,而不是移动 viewController 的主视图,只需像这样调整新创建的视图。

[UIView animateWithDuration:0.2 animations:^{
    //Just Replace Your Animation Code with This and see the difference.
    self.containerViewOfOtherObjects.frame = CGRectOffset(self.containerViewOfOtherObjects.frame, 0, movement);
}];