关于ios:scrollView iPhone中的Textfield

Textfield inside the scrollView iPhone

我是iPhone新手,我的应用程序中有10个文本域,位于scrollview中。
我需要的是当用户触摸文本字段时,scrollview应该以这种方式滚动,这样文本字段不应该在键盘后面。

帮我。

谢谢你对我的帮助。


覆盖像这样的textfield委托方法

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

//此方法将当前选定的文本字段移动到可见区域

1
2
3
4
5
6
7
8
9
10
11
12
13
-(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];
}

当你想解雇键盘覆盖这个委托时

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