【问题标题】:How to prevent scrollview bouncing when adjusting content for keyboardWillBeShown/keyboardWillBeHidden调整keyboardWillBeShown/keyboardWillBeHidden的内容时如何防止滚动视图弹跳
【发布时间】:2013-11-23 22:53:36
【问题描述】:

我有一个带有多个文本字段的滚动视图,包括一些位于相同 Y 位置的文本字段。我正在使用以下代码来调整滚动视图的偏移量/插图,以便活动文本字段始终可见 - 但如果用户已经在编辑一个字段,然后点击同一“行”中的另一个文本字段(相同的 Y 位置)滚动视图反弹/重新调整。有没有办法解决这个问题?

- (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 来阻止这种行为,并且动画 = NO。所以我假设使用 animated = YES 将实际工作启动到一个块或延迟实际设置的执行,然后当它 /does/ 实际执行该操作时,重新显示会立即将其重置为相同位置。

想法?

【问题讨论】:

    标签: ios uiscrollview uitextfield


    【解决方案1】:

    你可以像 contentOffsetY=_scrollView.contentOffset.y 一样存储滚动视图的当前内容偏移量;然后检查

    if(contentOffsetY==_scrollView.contentOffset.y)
    {
    //then should not change scrollviews content offset
    }
    else
    {
    //change content offset
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 2018-12-23
      • 2021-11-16
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多