我有一个UIScrollView,其中我有一个UITextField,所以我做的是在第三个文本字段,即密码,我向下滚动UIScrollView的偏移量:
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ((textField == self.emailTextField_ || textField == self.passwordTextField_)){
if (self.scrollView_.contentOffset.y != self.emailTextField_.frameY - self.emailTextField_.frameHeight/2){
shouldAdjustOffset = YES;
} else {
shouldAdjustOffset = NO;
}
if (shouldAdjustOffset){
[self.scrollView_ setContentOffset:CGPointMake(0, self.emailTextField_.frameY - self.emailTextField_.frameHeight/2) animated:YES];
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstNameTexField_){
[self.firstNameTexField_ resignFirstResponder];
[self.lastNameTextField_ becomeFirstResponder];
} else if (textField == self.lastNameTextField_){
[self.lastNameTextField_ resignFirstResponder];
[self.emailTextField_ becomeFirstResponder];
} else if (textField == self.emailTextField_){
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
} else if (textField == self.passwordTextField_){
[self signupButtonPressed:nil];
[self.passwordTextField_ resignFirstResponder];
}
return YES;
}
现在的问题是,当我在下一个文本字段时,它将内容偏移重置为0,当我这样做时:
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
。我怎样才能防止这种情况发生?
答案 0 :(得分:3)
在显示键盘之前,只需转动滚动视图滚动(becomeFirstResponder)并在其后启用滚动,
scrollView.scrollEnabled = NO;
[someControl becomeFirstResponder];
scrollView.scrollEnabled = YES;
[scrollview doTheScrollingHere];
答案 1 :(得分:0)
@Deepan你节省了我很多时间:)只有我建议的是覆盖 UITextField 方法 becomeFirstResponder 来检查如果它在 UIScrollView 中,如果禁用滚动片刻
- (BOOL)becomeFirstResponder
{
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:NO];
}
BOOL accept = [super becomeFirstResponder];
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:YES];
}
return accept
}