摘译自: 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;
}

本文介绍了一种方法,通过使用实例变量记录视图在键盘调整前的位置,并在文本字段返回时恢复到之前的状态。具体实现包括获取文本字段的边界、转换坐标和设置滚动视图内容偏移量。
506

被折叠的 条评论
为什么被折叠?



