2012-07-17 103 views
0

我有相當長的形式在ipad有uitextfields和uitextviews。當鍵盤使用鍵盤通知隱藏uitextfield時,我可以向上滾動。但是當uitextview變爲活動狀態並且它位於鍵盤下方時,它足夠滑動,以便我可以看到閃爍的光標。這是正常的行爲?如果不是,我怎麼可以向上滾動整個uitextview,以便在編輯時可以看到整個uitextview?ipad鍵盤隱藏uitextView

這裏是代碼..

-(void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:YES]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; 
} 

#pragma mark Keyboard Events 

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return NO; 
} 

#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 
-(void)keyboardWasShown:(NSNotification *)aNotification 
{ 

if (displayKeyboard==YES) { 
    return; 
} 
NSDictionary* info = [aNotification userInfo]; 
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; 
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
CGSize keyboardSize = [aValue CGRectValue].size; 


offset = _scrollView.contentOffset; 

CGRect viewFrame = _scrollView.frame; 

viewFrame.size.height -= keyboardSize.height-tabbarHt; 
_scrollView.frame = viewFrame; 

CGRect textFieldRect = [activeField frame]; 
textFieldRect.origin.y += 10; 
[_scrollView scrollRectToVisible: textFieldRect animated:YES]; 
displayKeyboard = YES; 

} 

-(void)keyboardWillBeHidden:(NSNotification *)aNotification 
{ 

if (!displayKeyboard) { 
    return; 
} 

_scrollView.frame = CGRectMake(0, 0, 1024, 655); 
_scrollView.contentOffset =offset; 
displayKeyboard = NO; 
} 

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField { 
activeField = textField; 
return YES; 
} 
+0

你必須設置在UITextViewDelegate方法滾動的方法 – Hiren 2012-07-17 09:29:31

+0

@CocoaMatters你能告訴我怎麼去做這件事嗎? – southpark 2012-07-17 09:34:55

+0

你如何爲UITextField做些什麼? – Hiren 2012-07-17 09:35:59

回答

0

給委託給你的UITextView

CGRect activeField; 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    activeField = textView.frame; 
} 

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField { 
    activeField = textField.frame; 
    return YES; 
} 

嘗試用這個代碼。每次只給出textField矩形大小。當你調用文本視圖你必須通過textview框架大小

+0

但activeField是一個UItextField對象...我應該爲Uitextview做一個類似的對象? – southpark 2012-07-17 09:53:41

+0

做一件事從UItextField和UItextView傳遞直接框架,而不是使新對象 – Hiren 2012-07-17 09:56:29

+0

Yup想通了。謝謝你的幫助。 – southpark 2012-07-17 09:59:55