我是 iPhone 新手,我的应用程序中有 10 个文本字段位于滚动视图中。我需要的是当用户触摸文本字段时,scrollview 应该以这种方式滚动,以便文本字段不应该在键盘后面。
帮我。
谢谢你对我的帮助。
像这样覆盖文本字段委托方法
- (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;
}