键盘出现时如何向上移动两个UIViews?

时间:2013-06-20 14:49:32

标签: ios uiview nsnotificationcenter

我有两个UIViews,UIView2有UITextView,可以放在用户喜欢添加Text标签的地方。当用户将UiTextView放在底部时,键盘会显示文本类型,UITextView会向上移动。而且效果很棒!我还需要移动UIView1下的UIView1。

UIView2是UIView1的属性,当UIView2的UITextView成为FirstRirstResponder时,我需要Notify 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];

1 个答案:

答案 0 :(得分:2)

您已添加键盘事件的通知,那么您需要做的是实现方法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];

 }