2012-08-28 43 views
1

我是iPhone新手,我的應用程序中有10個文本框在scrollview中。 我需要的是當用戶觸摸一個文本域時,scrollview應該以這種方式滾動,以便textfield不應該在鍵盤後面。TextView裏面的scrollView iPhone

幫幫我。

感謝您對我的幫助。

+0

** 1。**將當前視圖向上移動到可見區** 2。**或減少'U當鍵盤顯示時,可見房間的「IScrollView」大小。 – holex

回答

1

覆蓋文本框的委託方法像這樣

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self scrollViewToCenterOfScreen:textField]; 
} 

//此方法將當前所選文本字段的可見區域

-(void)scrollViewToCenterOfScreen:(UIView *)theView 
{ 
    CGFloat viewCenterY = theView.center.y; 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

    CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard   

    CGFloat y = viewCenterY - availableHeight/2.0; 
    if (y < 0) { 
     y = 0; 
    } 
    [scrollview setContentOffset:CGPointMake(0, y) animated:YES]; 
} 

而當你想關閉該鍵盤覆蓋此委託

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
     [textField resignFirstResponder]; 
     [self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES]; 
    return YES; 
}