Is it possible to disable floating headers in UITableView with UITableViewStylePlain?

前端 未结 29 1104
走了就别回头了
走了就别回头了 2020-11-29 15:02

I\'m using a UITableView to layout content \'pages\'. I\'m using the headers of the table view to layout certain images etc. and I\'d prefer it if they didn\'t

29条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 15:30

    Maybe you could use scrollViewDidScroll on the tableView and change the contentInset based on the current visible header.

    It seems to work for my use case!

    extension ViewController : UIScrollViewDelegate{
    
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            guard   let tableView = scrollView as? UITableView,
                let visible = tableView.indexPathsForVisibleRows,
                let first = visible.first else {
                return
            }
    
            let headerHeight = tableView.rectForHeader(inSection: first.section).size.height
            let offset =  max(min(0, -tableView.contentOffset.y), -headerHeight)
            self.tableView.contentInset = UIEdgeInsetsMake(offset, 0, -offset, 0)
       }
    }
    

提交回复
热议问题