How to resize a tableHeaderView of a UITableView?

后端 未结 19 2217
死守一世寂寞
死守一世寂寞 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:05

    This is only for when you use auto-layout and set translatesAutoresizingMaskIntoConstraints = false to a custom header view.

    The best and the simplest way is to override intrinsicContentSize. Internally UITableView uses intrinsicContentSize to decide its header/footer size. Once you have override intrinsicContentSize in your custom view, What you need to do is as below

    1. configure the custom header/footer view's layout(subviews)
    2. invoke invalidateIntrinsicContentSize()
    3. invoke tableView.setNeedsLayout() and tableView.layoutIfNeeded()

    Then the UITableView's header/footer will be updated as you want. No need to set the view nil or reset.

    One thing really interesting for the UITableView.tableHeaderView or .tableFooterView is that UIStackView loose its ability to manage its arrangedSubviews. If you want to use UIStackView as a tableHeaderView or tableFooterView, you have to embed the stackView in a UIView and override UIView's intrinsicContentSize.

提交回复
热议问题