Ios 如何在调整键盘内容时防止滚动视图反弹将显示/keyboardWillBeHidden
我有一个带有许多文本字段的滚动视图,包括一些位于相同Y位置的文本字段。我正在使用以下代码调整scrollview的偏移/插入,以便活动文本字段始终可见-但是如果用户已经编辑了一个字段,然后点击同一“行”(同一Y位置)中的另一个文本字段,则scrollview会反弹/重新调整。有没有办法解决这个问题Ios 如何在调整键盘内容时防止滚动视图反弹将显示/keyboardWillBeHidden,ios,uiscrollview,uitextfield,Ios,Uiscrollview,Uitextfield,我有一个带有许多文本字段的滚动视图,包括一些位于相同Y位置的文本字段。我正在使用以下代码调整scrollview的偏移/插入,以便活动文本字段始终可见-但是如果用户已经编辑了一个字段,然后点击同一“行”(同一Y位置)中的另一个文本字段,则scrollview会反弹/重新调整。有没有办法解决这个问题 - (void)keyboardWillBeShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
UIView *activeField = [self.view findFirstResponder];
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
aRect.size.height -= aRect.origin.y;
aRect.origin.y = 0;
CGPoint activeFieldOrigin = [activeField convertPoint:CGPointZero toView:self.view.window];
if (!CGRectContainsPoint(aRect, activeFieldOrigin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeFieldOrigin.y-kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
我可以通过执行setContentOffset:animated with animated=NO来防止这种行为。因此,我假设如果使用animated=YES,它会将实际工作踢到一个块上,或者延迟实际设置的执行,然后当它/确实/实际这样做时,重新显示会立即将其重置回相同的位置
想法?您可以像contentOffsetY=\u scrollview.contentoffset.y;然后检查
if(contentOffsetY==_scrollView.contentOffset.y)
{
//then should not change scrollviews content offset
}
else
{
//change content offset
}