UIScrollView中的UITextField键盘遮挡问题的最简单解决办法
jopen
11年前
两步:
1重载textFieldDidBeginEditing
- (void)textFieldDidBeginEditing:(UITextField *)textField { foucsTextField = textField; s_mainScrollView.contentSize = CGSizeMake(__SCREEN_WIDTH,__SCREEN_HEIGHT +216);//原始滑动距离增加键盘高度 CGPoint pt = [textField convertPoint:CGPointMake(0, 0) toView:s_mainScrollView];//把当前的textField的坐标映射到scrollview上 if(s_mainScrollView.contentOffset.y-pt.y+NAVIGATION_BAR_HEIGHT<=0)//判断最上面不要去滚动 [s_mainScrollView setContentOffset:CGPointMake(0, pt.y-NAVIGATION_BAR_HEIGHT) animated:YES];//华东 }
2:重载textFieldShouldReturn
- (BOOL)textFieldShouldReturn:(UITextField*)theTextField { { [theTextField resignFirstResponder]; //开始动画 [UIView beginAnimations:nil context:nil]; //设定动画持续时间 [UIView setAnimationDuration:0.3]; s_mainScrollView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, __SCREEN_WIDTH, __SCREEN_HEIGHT); s_mainScrollView.contentSize = CGSizeMake(__SCREEN_WIDTH,__SCREEN_HEIGHT); //动画结束 [UIView commitAnimations]; } return YES; }
这样就可以自动的滑动scrollview里面的输入控件了.