How to resize a tableHeaderView of a UITableView?

后端 未结 19 2210
死守一世寂寞
死守一世寂寞 2020-11-29 16:14

I\'m having trouble resizing a tableHeaderView. It simple doesn\'t work.

1) Create a UITableView and UIView (100 x 320 px);

2) Set the UIView as tableHeaderV

相关标签:
19条回答
  • 2020-11-29 17:13

    For swift 5 Tested code

    override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
    
            guard let headerView = self.tblProfile.tableHeaderView else {
                return
            }
    
            let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    
            if headerView.frame.size.height != size.height {
                headerView.frame.size.height = size.height
                self.tblProfile.tableHeaderView = headerView
                self.tblProfile.layoutIfNeeded()
            }
        }
    

    Note : You need to give all subview's constraints form top, bottom, leading, trailing. So it will get whole required size.

    Reference taken from : https://useyourloaf.com/blog/variable-height-table-view-header/

    0 讨论(0)
提交回复
热议问题