【问题标题】:Nav Bar invisible when keyboard is displayed显示键盘时导航栏不可见
【发布时间】:2016-12-29 23:10:23
【问题描述】:

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之外的所有东西,或者在使用[self animateTextView: textField up: YES];后将navBar保持在它的位置?

【问题讨论】:

  • 为什么?你为什么要这样设计你的视图?为什么不使用UINavigationController?你为什么使用beginAnimations: API?这是什么,iOS / iphoneos 3?

标签: ios objective-c navbar


【解决方案1】:

那是因为您的主视图中有自定义 NavigationBar。 在 Storyboard 中的 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);
}];

【讨论】:

  • 谢谢赛义德,我已经添加了,我是否需要将self.view.frame = CGRectOffset(self.view.frame, 0, movement); 替换为答案中发布的行?或者也删除[UIView beginAnimations: @"anim" context: nil];...
  • 没有。就这一行。 self.view.frame = CGRectOffset(self.view.frame, 0, movement);
  • 我已经尝试过了,但这就像快速向上和向下移动新添加的视图到它的原始位置
  • 让我补充一点。
  • 用这行代码替换我的代码以获得流畅的动画[UIView beginAnimations: @"anim" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations];
猜你喜欢
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多