[英]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.