UIScrollView setContentOffset:animated 在 iOS11 中不工作
UIScrollView setContentOffset:animated not working in iOS11
我添加了一种使滚动视图滚动到其顶部的方法。这在 iOS10 中按预期工作。但是当我用 Xcode 9 升级到 iOS11 时,它不起作用。 iOS11.
是否缺少任何内容
以下代码有效:
[self setContentOffset:CGPointZero animated:YES];
在 OP 发表评论后更新问题(更新)
我找到了 setContentOffset:animated 不起作用的原因,我使用 UITableView 并在我的 tableview 中制作了一些自定义单元格,tableview 有一个 属性 "estimatedRowHeight" ,我们应该制作这个 属性 等于“0”,我想当 iOS11 计算它的内容大小时它将使用“estimatedRowHeight”,如果我们不设置这个 属性 ,它将使用系统默认值。
这个很有用,应该更显眼
试试这个
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.25 animations:^{
[self setContentOffset:CGPointZero animated:YES];
}];
});
正如@Alejandro 之前提到的,iOS 11 确实存在setContentOffset 和动画问题。
我找到了如何让它工作:
Swift 3, 4:
if #available(iOS 11.0, *) {
let offset = CGPoint(x: -scrollView.adjustedContentInset.left, y: -scrollView.adjustedContentInset.top)
scrollView.setContentOffset(offset, animated: false)
} else {
let offset = CGPoint(x: -scrollView.contentInset.left, y: -scrollView.contentInset.top)
scrollView.setContentOffset(offset, animated: false)
}
解决此问题的方法(至少对于我的应用程序)是将 "beginUpdates" 和 "endUpdates" 添加到 UITableView 并选择 "animated:NO" ,如下所示:
[mySuperbTableView beginUpdates];
[mySuperbTableView setContentOffset:CGpointMake(0,0) animated:NO];
[mySuperbTableView endUpdates];
您可以将 CGPoint 设置为您需要的任何值。 ;-)
在对 UITableView 调用此更新后注意对数据源进行任何更改,否则它会崩溃!
感谢 Pavithra Pavi 在另一个 post 中正确回答了这个问题。
编辑:
好的,它不会一直工作。
更好的解决方案(如果您选择不为 tableview 设置动画)是设置
table.estimatedRowHeight = 0;
在你的委托中。
它似乎修复了 setContentOffset:animated:NO 的行为。
你必须做
self.view.layoutIfNeeded()
在 setContentOffset 之前。
它更新了 UIScrollView 的 contentSize,然后 contentOffset 可用。
(SWIFT 4)
我有一个类似的问题,问题是动画:真实。
因此,您可以尝试手动执行此操作:
view.layoutIfNeeded()
UIView.animate(withDuration: 0.2) {
self.scroll.setContentOffset(.zero, animated: false)
}
希望对您有所帮助。它为我修复了
为滚动视图设置 estimatedRowHeight = 0、estimatedSectionHeaderHeight = 0、estimatedSectionFooterHeight = 0 解决了这个问题。每当更改内容偏移量时,UIKit 会计算每个部分的行高、页眉和页脚以滚动到新的偏移量。 estimatedRowHeight, estimatedSectionHeaderHeight, estimatedSectionFooterHeight 默认为-1
试试这个
if tableView.numberOfRows(inSection: 0) > 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
我添加了一种使滚动视图滚动到其顶部的方法。这在 iOS10 中按预期工作。但是当我用 Xcode 9 升级到 iOS11 时,它不起作用。 iOS11.
是否缺少任何内容
以下代码有效:
[self setContentOffset:CGPointZero animated:YES];
在 OP 发表评论后更新问题(更新)
我找到了 setContentOffset:animated 不起作用的原因,我使用 UITableView 并在我的 tableview 中制作了一些自定义单元格,tableview 有一个 属性 "estimatedRowHeight" ,我们应该制作这个 属性 等于“0”,我想当 iOS11 计算它的内容大小时它将使用“estimatedRowHeight”,如果我们不设置这个 属性 ,它将使用系统默认值。
这个很有用,应该更显眼
试试这个
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.25 animations:^{
[self setContentOffset:CGPointZero animated:YES];
}];
});
正如@Alejandro 之前提到的,iOS 11 确实存在setContentOffset 和动画问题。
我找到了如何让它工作:
Swift 3, 4:
if #available(iOS 11.0, *) {
let offset = CGPoint(x: -scrollView.adjustedContentInset.left, y: -scrollView.adjustedContentInset.top)
scrollView.setContentOffset(offset, animated: false)
} else {
let offset = CGPoint(x: -scrollView.contentInset.left, y: -scrollView.contentInset.top)
scrollView.setContentOffset(offset, animated: false)
}
解决此问题的方法(至少对于我的应用程序)是将 "beginUpdates" 和 "endUpdates" 添加到 UITableView 并选择 "animated:NO" ,如下所示:
[mySuperbTableView beginUpdates];
[mySuperbTableView setContentOffset:CGpointMake(0,0) animated:NO];
[mySuperbTableView endUpdates];
您可以将 CGPoint 设置为您需要的任何值。 ;-)
在对 UITableView 调用此更新后注意对数据源进行任何更改,否则它会崩溃!
感谢 Pavithra Pavi 在另一个 post 中正确回答了这个问题。
编辑: 好的,它不会一直工作。 更好的解决方案(如果您选择不为 tableview 设置动画)是设置 table.estimatedRowHeight = 0;
在你的委托中。
它似乎修复了 setContentOffset:animated:NO 的行为。
你必须做
self.view.layoutIfNeeded()
在 setContentOffset 之前。 它更新了 UIScrollView 的 contentSize,然后 contentOffset 可用。
(SWIFT 4) 我有一个类似的问题,问题是动画:真实。 因此,您可以尝试手动执行此操作:
view.layoutIfNeeded()
UIView.animate(withDuration: 0.2) {
self.scroll.setContentOffset(.zero, animated: false)
}
希望对您有所帮助。它为我修复了
为滚动视图设置 estimatedRowHeight = 0、estimatedSectionHeaderHeight = 0、estimatedSectionFooterHeight = 0 解决了这个问题。每当更改内容偏移量时,UIKit 会计算每个部分的行高、页眉和页脚以滚动到新的偏移量。 estimatedRowHeight, estimatedSectionHeaderHeight, estimatedSectionFooterHeight 默认为-1
试试这个
if tableView.numberOfRows(inSection: 0) > 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}