0

私には2つのUIViewがあり、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];
4

1 に答える 1

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];

 }
于 2013-06-20T15:01:42.797 に答える