UIScrollView setContentOffset:动画无法在iOS11中使用

Jas*_*Liu 4 objective-c ios11 xcode9

我添加了一种使滚动视图滚动到其顶部的方法。这在iOS10中按预期工作。但是,当我使用Xcode 9升级到iOS11时,它无法正常工作。是否有iOS11缺少的任何内容。
下面的代码正在工作:

[self setContentOffset:CGPointZero animated:YES];
Run Code Online (Sandbox Code Playgroud)

小智 8

你所要做的

self.view.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)

在setContentOffset之前。它更新UIScrollView的contentSize,然后contentOffset可用。


R. *_*han 7

尝试这个

dispatch_async(dispatch_get_main_queue(), ^{
    [UIView animateWithDuration:.25 animations:^{
        [self setContentOffset:CGPointZero animated:YES];
    }];
});
Run Code Online (Sandbox Code Playgroud)

  • 我找到了 setContentOffset:animated 不起作用的原因,我使用 UITableView 并在我的 tableview 中创建了一些自定义单元格,tableview 有一个属性“estimatedRowHeight”,我们应该使这个属性等于“0”,我猜当 iOS11 计算它的内容时size 它将使用“estimatedRowHeight”,如果我们不设置此属性,它将使用系统默认值。 (9认同)
  • @JasonLiu 请将此作为答案。这是默认估计行高度现在咬我的两倍! (2认同)

小智 6

为滚动视图设置estimatedRowHeight = 0、estimatedSectionHeaderHeight = 0、estimatedSectionFooterHeight = 0 解决了这个问题。每当内容偏移量发生变化时,UIKit 会计算每个部分的行、页眉和页脚的高度以滚动到新的偏移量。EstimatedRowHeight、estimatedSectionHeaderHeight、estimatedSectionFooterHeight 默认为 -1


Pab*_*nco 5

(SWIFT 4) 我有一个类似的问题,问题是动画:true。因此,您可以尝试通过手动执行以下操作:

view.layoutIfNeeded()
UIView.animate(withDuration: 0.2) {
   self.scroll.setContentOffset(.zero, animated: false)
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。它为我固定