0
私のアプリでは、画面の下部にUITextViewがあります。だから、私は次のコードを行うが、問題は、場合によっては、UITextViewでクリックしたテキストがキーボードの下にある場合にのみ、キーボードの上にスクロールすることがあります。 (のviewDidLoadで) キーボードがときどきUITextViewをブロックする
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification object:nil];
方法:ViewDidUnloadで :
-(void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary *info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [textView frame];
viewFrame.size.height -= keyboardSize.height;
textView.frame = viewFrame;
// Scroll the active text field into view.
//CGRect textFieldRect = [activeField frame];
[textView scrollRectToVisible:viewFrame animated:YES];
}
-(void)keyboardWasHidden:(NSNotification*)aNotification {
// Reset the height of the scroll view to its original value
CGRect viewFrame = [textView frame];
viewFrame.size.height += keyboardSize.height;
textView.frame = viewFrame;
}
がどのように私はNSNotificationsの登録を解除
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
ここ
は私がNSNotificationsを登録する方法であります
誰かが間違っていますか?
ありがとうございます!
場合によってはスクロールが行われない場合がありますが、フレームの計算と移動を確認してください。それは間違っているかもしれません。 – cocoakomali
NSLogはどうすればよいですか?これらの値はすべて非常に混乱しているので、私は投稿します。多分私はそれらの値を掲示するときに他の誰かが私の問題を見るでしょう。 –
元のビューフレーム、キーボードサイズ、最後に変更されたビューフレームを印刷する必要があります。 – cocoakomali