我有很多 View ,每个 View 都有很多 subview 。 我试图为每个 subview 指定
scrollsToTop = false
但是当我按下状态栏时,我的 TableView
没有滚动到顶部。
我想我错过了一些观点......
我知道有一个类似的帖子,但它没有回答(UITableView scroll to top when tapping status bar at top of the screen)
所以我决定实现这个功能:
override func viewDidLoad() {
super.viewDidLoad()
checkForScrollViewInView(self.view)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.scrollsToTop = true
}
func checkForScrollViewInView(view:UIView) {
for subview in view.subviews as [UIView] {
if subview.isKindOfClass(UITextView) {
(subview as! UITextView).scrollsToTop = false
}
if subview.isKindOfClass(UIScrollView) {
(subview as! UIScrollView).scrollsToTop = false
}
if subview.isKindOfClass(UITableView) {
(subview as! UITableView).scrollsToTop = false
}
if (subview.subviews.count > 0) {
self.checkForScrollViewInView(subview)
}
}
}
但是也行不通。我不知道我错过了什么。
最佳答案
您可能会遇到两个问题:
- 您有多个
UIScrollView
实例。您应该为父级UIScrollView
禁用scrollsToTop
(例如self.collectionView?.scrollsToTop = false
inviewDidLoad
)或继承自它的对象(如UITableView
或UICollectionView
)。
来自 Apple scrollViewShouldScrollToTop(_:)
If the delegate doesn’t implement this method, true is assumed. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.
- 如果您的层次结构是高度自定义的,则不会调用方法
scrollViewShouldScrollToTop
。你应该检查为什么会发生。否则你将不得不做一些肮脏的工作:添加一些手势并处理它们。
有趣的链接:
Scroll to top of UITableView by tapping status bar
Why doesn't UIScrollView/UITableview respond to taps on the status bar, and scroll to the top?
关于ios - 通过点击状态栏滚动到顶部 TableView 不起作用(有很多 subview )SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523220/