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

前端 未结 29 1097
走了就别回头了
走了就别回头了 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:27

    A variation on @samvermette's solution:

    /// Allows for disabling scrolling headers in plain-styled tableviews
    extension UITableView {
    
        static let shouldScrollSectionHeadersDummyViewHeight = CGFloat(40)
    
        var shouldScrollSectionHeaders: Bool {
            set {
                if newValue {
                    tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: UITableView.shouldScrollSectionHeadersDummyViewHeight))
                    contentInset = UIEdgeInsets(top: -UITableView.shouldScrollSectionHeadersDummyViewHeight, left: 0, bottom: 0, right: 0)
                } else {
                    tableHeaderView = nil
                    contentInset = .zero
                }
            }
    
            get {
                return tableHeaderView != nil && contentInset.top == UITableView.shouldScrollSectionHeadersDummyViewHeight
            }
        }
    
    }
    

提交回复
热议问题