2010-06-15 157 views
1

我有一個UIScrollView,在屏幕的下半部分是一個文本框。當我選擇文本框時,鍵盤顯示在文本框上方。 UIScrollView中的UIView不夠大,無法向上滾動併爲鍵盤釋放一些空間。所以我試圖通過編程擴大UIView。 這就是我得到的:UIScrollView滾動問題

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 


UIScrollView *scrollView = (UIScrollView *)[[self view] viewWithTag: 2]; 
UIView *contentview = [[self view] viewWithTag: 3]; 

CGRect rect = contentview.frame; 

rect.size.height = 650; 
contentview.frame = rect; 

[scrollView setContentSize: contentview.frame.size]; 

//first try 
rect.origin.y = 450; 
[scrollView scrollRectToVisible: rect animated: YES]; 

//second try 
CGPoint point = CGPointMake(0, 450); 
[scrollView setContentOffset: point animated: YES]; 

return YES; 
} 

謝謝。

回答

3

您可以註冊以在您的視圖控制器中接收UIKeyboardWillShowNotification。然後將視圖控制器的視圖框向上移動通知中提供的鍵盤大小。

CGRect frame = self.view.frame; 
frame.origin.y -= keyboard.size.height; 
self.view.frame = frame; 

對UIKeyboardWillHideNotification做相反的處理。