【问题标题】:How to move up two UIViews when keyboard appears?键盘出现时如何向上移动两个 UIView?
【发布时间】:2013-06-20 14:49:31
【问题描述】:

我有两个 UIViews 和 UIView2 有 UITextView 可以放置在用户喜欢添加文本标签的任何地方。当用户将 UiTextView 放在底部时,键盘出现在输入文本并且 UITextView 向上移动。而且效果很好!我还需要移动 UIView1 下的 UIView1。

UIView2 是 UIView1 的属性,当 UIView2 的 UITextView 成为FirstResponder 时,我需要通知 UIView1 为其执行 Move Up 方法。

 [[NSNotificationCenter defaultCenter] addObserver:self.drawingView
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self.drawingView
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

【问题讨论】:

  • 您的控制器需要处理通知,而不是视图。然后控制器移动视图。你看过这个问题的无数其他答案吗?请参阅相关问题列表。

标签: ios uiview nsnotificationcenter


【解决方案1】:

您已经为键盘事件添加了通知,那么您要做的就是实现方法keyboardWillShow 和keyboardWillHide。见下文

 - (void)keyboardWillShow:(NSNotification *)notification {
/*
 Reduce the size of the text view so that it's not obscured by the keyboard.
 Animate the resize so that it's in sync with the appearance of the keyboard.
 */
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[self moveCommentBarViewWithKeyBoardHeight:keyboardRect.size.height withDuration:animationDuration];

}

 - (void)keyboardWillHide:(NSNotification *)notification {
   NSDictionary* userInfo = [notification userInfo];
/*
   Restore the size of the text view (fill self's view).
 Animate the resize so that it's in sync with the disappearance of the keyboard.
 */
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveCommentBarViewWithKeyBoardHeight:0 withDuration:animationDuration];
}

希望它可以帮助:)

PS:添加此通知时,最好添加到方法 -(void)viewWillAppear:(BOOL)animite。并使用

 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

在 viewWillDisappear 时移除观察。

 -(void)moveCommentBarViewWithKeyBoardHeight:(CGFloat)kHeighy withDuration:(NSTimeInterval)animationD
 {
 CGRect tempRect = commentEditedBarView.frame;
 [UIView beginAnimations:@"Animation" context:nil];
 [UIView setAnimationDuration:animationD];

 [commentEditedBarView setFrame:CGRectMake(tempRect.origin.x, self.view.frame.size.height-kHeighy-tempRect.size.height, tempRect.size.width, tempRect.size.height)];
 [UIView commitAnimations];

 }

【讨论】:

  • 谢谢,但我有那些方法keyboardWillShow 和keyboardWillHide。我通过 NSNotification 从 UIView1 调用 UIView2 的这些方法。但我尝试在 UIView1 中做同样的事情,但 UITextView 属于 UIView2。
  • 不,它不是你所拥有的,你必须实现你的方法。看我给你的那些方法。好的,我忘了给你移动视图的方法。我现在添加到我的答案中。
  • 正如我所说,它适用于 UIView2(self.drawingView) 但我无法移动 UIView2
  • 看我的方法。你可以像这样重写我的方法 -(void)moveCommentBarViewWithKeyBoardHeight:(UIView*)aView 来获取你要移动的 textField 的超级视图的句柄,这样无论你放在哪里,你都可以让超级视图移动。
  • 非常感谢! :)大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2013-05-21
  • 2014-11-15
  • 2015-02-15
  • 2011-07-10
  • 2015-10-17
  • 1970-01-01
相关资源
最近更新 更多