摘译自: http://stackoverflow.com/questions/484855/how-programatically-move-a-uiscrollview-to-focus-in-a-control-above-keyboard
使用一个实例变量记录视图在为键盘调整之前的位置,以用于当UITextField返回时恢复到之前的状态。
//header @interface TheViewController : UIViewController <UITextFieldDelegate> { CGPoint svos; } //implementation - (void)textFieldDidBeginEditing:(UITextField *)textField { svos = scrollView.contentOffset; CGPoint pt; CGRect rc = [textField bounds]; rc = [textField convertRect:rc toView:scrollView]; pt = rc.origin; pt.x = 0; pt.y -= 60; [scrollView setContentOffset:pt animated:YES]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [scrollView setContentOffset:svos animated:YES]; [textField resignFirstResponder]; return YES; }