iOS - 移动视图时重叠的界面元素

时间:2012-05-08 21:39:04

标签: ios uitableview uiview uitextfield

我的应用程序上有以下界面布局:

enter image description here

当我点击一个文本字段时(如果你舔添加标签添加了一个新的文本字段)我将我的视图向上滚动,以免被键盘阻挡,这样用户就可以正确地看到他正在键入的内容,但是以下内容当我这样做时发生:

enter image description here enter image description here

这是我用来滚动的代码:

- (void)keyboardWillShow:(NSNotification *)notification {
  if (self.keyboardIsShown) {
    return;
  }

  NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey];
  CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size;

  NSTimeInterval animationDuration = 0.3;

  CGRect frame = self.view.frame;

  frame.origin.y -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
  frame.size.height += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;

  [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:animationDuration];
  self.view.frame = frame;
  [UIView commitAnimations];

  self.keyboardIsShown = YES;
}

- (void)keyboardWillHide:(NSNotification *)notification {
  NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey];
  CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size;

  NSTimeInterval animationDuration = 0.3;

  CGRect frame = self.view.frame;

  frame.origin.y += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
  frame.size.height -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;

  [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:animationDuration];
  self.view.frame = frame;
  [UIView commitAnimations];

  self.keyboardIsShown = NO;
}

任何想法如何让视图元素显示在导航栏后面或消失或其他可以正常工作的解决方法?

2 个答案:

答案 0 :(得分:1)

实现目标的快捷方法是将导航栏放在视图树中的“内容”视图下方。这样导航栏将保持在顶部。如果您动态添加内容。确保将它们添加为导航栏后面的视图的子视图。

//编辑

正如评论中所建议的那样:

self.view.clipsToBounds = YES;
CGRect frame = self.yourWrappingView.frame;
NSUInteger distanceToMove = 200;
self.yourWrappingView.frame = CGRectMake(frame.origin.x - distanceToMove, frame.origin.y, frame.size.width, frame.size.height);

这适合你的伴侣:)

答案 1 :(得分:0)

可能你没有使用真正的UINavigationController。但你应该。所以你不会有这些问题和正确的调整大小行为。但要在当前架构中解决它:

  • 确保导航栏是nib文件/故事板中最顶层的对象
  • 为您的UINavigationBar添加IBOutlet,如果您动态插入新视图,请使用[self insertSubview: newView belowSubview: navigationBarOutlet];