这就是我实现代码的方式:
1) 我的视图是 UIScrollView。
2) 我的视图中有多个 UITextField 并使用标签 ID 将它们区分开来。当用户点击 UITextField 时,会调用此委托来移动 UIScrollView:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
switch (textField.tag)
{
case 0:
[myScrollView setContentOffset:CGPointMake(0, 18) animated:YES];
break;
case 1:
[myScrollView setContentOffset:CGPointMake(0, 56) animated:YES];
break;
case 2:
[myScrollView setContentOffset:CGPointMake(0, 94) animated:YES];
break;
case 3:
[myScrollView setContentOffset:CGPointMake(0, 132) animated:YES];
break;
case 4:
[myScrollView setContentOffset:CGPointMake(0, 170) animated:YES];
break;
case 5:
[myScrollView setContentOffset:CGPointMake(0, 208) animated:YES];
break;
case 6:
[myScrollView setContentOffset:CGPointMake(0, 246) animated:YES];
break;
case 7:
[myScrollView setContentOffset:CGPointMake(0, 284) animated:YES];
break;
case 8:
[myScrollView setContentOffset:CGPointMake(0, 322) animated:YES];
break;
default:
break;
}
return YES;
}
3)我也有 UITextView 并使用这个委托来达到同样的效果:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if(textView.tag == 0)
{
[myScrollView setContentOffset:CGPointMake(0, 360) animated:YES];
// do some stuff...
}
if(textView.tag == 1)
{
[myScrollView setContentOffset:CGPointMake(0, 504) animated:YES];
// do some stuff...
}
return YES;
}
注意:这些CGPointMake(0, 123)
值取决于您自己的文本字段坐标。