[英]Scroll to Table View Header top
我有一个UITableView
标题,它占据了屏幕的整个框架。
每次应用程序进入后台,然后返回前台/活动时,我希望将屏幕设置回标题 top 。
我只看到了与滚动到表格顶部有关的答案,这些答案使用self.tableView.scrollToRow(at: top, at: .top, animated: true)
类的答案,但这并没有将不起作用,因为它只是向后滚动到第0行0 UITableView
中的标头不顶UITableView
。
有什么想法吗?
谢谢!
编辑:试过这个
override func viewWillAppear(_ animated: Bool) {
self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)
}
这是一些值得注意的表设置代码:
// Set up Frames
let headerFrame: CGRect = UIScreen.main.bounds
// Background Table
self.tableView.isPagingEnabled = true
// Set up Table Header
let header = UIView(frame: headerFrame)
self.tableView.tableHeaderView = header
TableView 是一个滚动视图子类,所以我想这应该有效:
self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)
当应用程序进入前台时,不会调用 ViewWillAppear。 为UIApplicationWillEnterForegroundNotification
注册您的 VC
您可以滚动到节标题,尝试以下代码:
DispatchQueue.main.async {
let indexPath = IndexPath(row: NSNotFound, section: yourSectionIndex)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
对于行添加: NSNotFound
根据您的要求更改“scrollToRow”中的 .top/.bottom 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.