滚动到表视图标题顶部



我有一个占用整个屏幕帧的UITableView标题。

每次应用程序进入后台,然后回到前台/活动,我希望屏幕被设置回标题顶部

我只看到与滚动到表格顶部有关的答案,这些答案使用像self.tableView.scrollToRow(at: top, at: .top, animated: true)这样的东西,但这不起作用,因为它只滚动回UITableView的第0行第0节,而不是UITableView标题的顶部。

任何想法?

谢谢!

Edit: Tried this

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不会在app进入前台时被调用。为UIApplicationWillEnterForegroundNotification注册VC

可以滚动到Section Header,试试下面的代码:

DispatchQueue.main.async {
    let indexPath = IndexPath(row: NSNotFound, section: yourSectionIndex)
    self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}

For Row add: NSNotFound

直变化上。底部为"scrolltorow";

最新更新