简体   繁体   中英

Scroll to Table View Header top

I have a UITableView header that takes up the full frame of the screen.

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 .

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 .

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:

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

ViewWillAppear does not get called when app enters foreground. Register you VC for UIApplicationWillEnterForegroundNotification

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

Change .top/.bottom in "scrollToRow" as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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