0
在我的應用程序中,我有一個UITextView,它位於屏幕的底部。所以我做的是下面的代碼,但問題是,有時如果單擊UITextView的文本位於鍵盤下方,它將正確滾動到鍵盤上方。鍵盤有時會阻止UITextView
這裏是我註冊NSNotifications: (在viewDidLoad中)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification object:nil];
的方法:
-(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: 在ViewDidUnload:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
有沒有人看到任何錯誤?
謝謝!
如果只是有時不會發生滾動,請檢查您的框架計算和移動。它可能會出錯。 – cocoakomali 2012-03-04 03:49:13
我應該怎麼發佈NSLog,因爲所有這些值都很混亂。當我發佈這些值時,也許別人會看到我的問題。 – 2012-03-04 04:22:54
您應該打印原始視圖框架,鍵盤大小和最終修改後的視圖框架。 – cocoakomali 2012-03-09 18:23:39