简体   繁体   English

滚动到表格视图标题顶部

[英]Scroll to Table View Header top

I have a UITableView header that takes up the full frame of the screen.我有一个UITableView标题,它占据了屏幕的整个框架。

Every time the app goes in the background, and then comes back into the foreground/active, I would like the screen to be set back to the header top .每次应用程序进入后台,然后返回前台/活动时,我希望将屏幕设置回标题 top

I've only seen answers having to do with scrolling to the top of the table with something like these answers that use something like self.tableView.scrollToRow(at: top, at: .top, animated: true) , but that doesn't work because it only scrolls back to Section 0 Row 0 of the UITableView not the top of the header of the UITableView .我只看到了与滚动到表格顶部有关的答案,这些答案使用self.tableView.scrollToRow(at: top, at: .top, animated: true)类的答案,但这并没有将不起作用,因为它只是向后滚动到第0行0 UITableView中的标头不顶UITableView

Any ideas?有什么想法吗?

Thanks!谢谢!

Edit : Tried this编辑:试过这个

override func viewWillAppear(_ animated: Bool) {
    self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)
}

And here is some noteworthy table setup code:这是一些值得注意的表设置代码:

// 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 is a scroll view subclass, so I imagine this should work: TableView 是一个滚动视图子类,所以我想这应该有效:

 self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)

ViewWillAppear does not get called when app enters foreground.当应用程序进入前台时,不会调用 ViewWillAppear。 Register you VC for UIApplicationWillEnterForegroundNotificationUIApplicationWillEnterForegroundNotification注册您的 VC

You can scroll to Section Header, try following code:您可以滚动到节标题,尝试以下代码:

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

For Row add: NSNotFound对于行添加: NSNotFound

Change .top/.bottom in "scrollToRow" as per your requirement.根据您的要求更改“scrollToRow”中的 .top/.bottom 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM