2013-05-01 34 views
0

我有一個nsarray的textfields和另一個textarea。我還有一個帶有下一個和上一個按鈕的工具欄來管理我的字段之間的導航。我在textfieldDidBeginEditing中創建了一個函數,而在textfieldDidEndEditing中創建了另一個函數,但它無法正常工作。 我的功能是:如何在uiscrollview上的許多textfield和textarea之間滾動?

如果(textField.frame.origin.y> 180){

[UIScrollView beginAnimations:nil context:NULL]; 

    [UIScrollView setAnimationDelegate:self]; 

    [UIScrollView setAnimationDuration:0.4]; 

    [UIScrollView setAnimationBeginsFromCurrentState:YES]; 

    CGFloat updatedY = 150; 

    self.scrollView.frame = CGRectMake(self.scrollView.frame.origin.x,self.scrollView.frame.origin.y - updatedY, self.scrollView.frame.size.width, 
             self.scrollView.frame.size.height); 

    [UIScrollView commitAnimations]; 

} 

任何幫助將不勝感激。

回答

0

請儘量使用這一個

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 
     [UIView beginAnimations:@"" context:nil]; 
     [UIView setAnimationDuration:.30]; 

     [self scrollViewToCenterOfScreen:textView]; 

     [UIView commitAnimations]; 
     return YES; 
} 


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
     [UIView beginAnimations:@"" context:nil]; 
     [UIView setAnimationDuration:.30]; 

     [self scrollViewToCenterOfScreen:textField]; 

     [UIView commitAnimations]; 

     return YES; 

} 


- (void)scrollViewToCenterOfScreen:(UIView *)theView 
{ 
    CGFloat y ; 
    CGFloat viewCenterY = theView.center.y; 

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

    CGFloat avaliableHeight = applicationFrame.size.height - 250; 

    y = viewCenterY - avaliableHeight/2.0f; 

    if (y < 0) 
    { 
     y = 0; 
    } 
    [scrollView setContentOffset:CGPointMake(0, y)]; 
} 
+0

感謝Dharmbir,您的解決方案適用於縱向模式,但不適用於橫向模式。 – 2013-05-01 09:26:20

+0

您的歡迎Dear.And是它僅適用於肖像模式。 – 2013-05-01 09:27:37

+0

接受哪一個答案是我親愛的。你接受了kirti的答案。如果你有答案,那麼請接受我的答案。 – 2013-05-01 09:28:45

1

我想你想要鍵盤處理程序。我正在使用的是一個非常好的鍵盤處理程序。

Keyboard Handler by Sukhpal Singh

這是我的一個好朋友一個很好的教程,你可以使用此功能。

對於使用這個,你只需要編寫一行代碼。

AutoScroller * scroller=[AutoScroller addAutoScrollTo:self.scrollView isDoneToolBarNeeded:NO]; 

是它完成。

+0

此外,如果能解決您的問題,將其標記爲答案。並幫助社區解決他人的問題。 – 2013-05-01 07:50:25

+0

感謝Avtar的幫助,但似乎這種方法工作只isdoneToolBarneeded沒有,我想申請我自己的工具欄與下一個和以前的選項 – 2013-05-01 09:29:37

+0

不,它也適用於完成工具欄只是使用。 [AutoScroller addAutoScrollTo:self.scrollView isDoneToolBarNeeded:Yes]。但是,它會在鍵盤上方添加一個帶有完成按鈕的工具欄作爲輸入附件視圖,並在完成按鈕上單擊時,鍵盤將隱藏。如果你想添加自己的工具欄,那麼恐怕這個方法對你來說不會有用,因爲它處理鍵盤上的所有代表。我不認爲你需要通過使用這個下一個和上一個鍵。 – 2013-05-01 09:59:09

1

試試這個代碼:

- (void)textFieldDidBeginEditing:(UITextField *)textField; 
{ 


     [clientscrollview setContentOffset:CGPointMake(0, textField.center.y-180) animated:YES]; 



} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [clientscrollview setContentOffset:CGPointMake(0, 0) animated:YES]; 
    [textField resignFirstResponder]; 
    return YES; 
} 
- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 

     [clientscrollview setContentOffset:CGPointMake(0,textView.center.y-180) animated:YES]; 



} 
- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    [clientscrollview resignFirstResponder]; 
    [clientscrollview setContentOffset:CGPointMake(0,0) animated:YES]; 

}