Ethan Zhao
Ethan Zhao

Reputation: 249

How to change height of view in tableview header?

I have a tableView header. I put a view and several items in. However, when a button is tapped, some objects in the view are hidden so that the view doesn't contain it as well anymore: enter image description here

enter image description here

How would I change the view height to match the items in the view?

Upvotes: 1

Views: 3012

Answers (3)

George
George

Reputation: 768

Try dynamically sizing the headerview by overriding viewDidLayoutSubviews() and see if that works :)

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Dynamic sizing for the header view
    if let headerView = tableView.tableHeaderView {
        let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
        var headerFrame = headerView.frame

        // If we don't have this check, viewDidLayoutSubviews() will get
        // repeatedly, causing the app to hang.
        if height != headerFrame.size.height {
            headerFrame.size.height = height
            headerView.frame = headerFrame
            tableView.tableHeaderView = headerView
        }
    }
}

Upvotes: 1

Sudipto Roy
Sudipto Roy

Reputation: 6795

Implementing this delegate function can help you...

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return Your_View.frame.height Or Others
}

Upvotes: 1

Rahul
Rahul

Reputation: 2100

You need to set the sectionHeaderHeight property in viewDidLoad. You will need to have tableView reference if you are using storyboard unless if your ViewCOntroller is of type TableViewController then just use below code.

 # Set the Header height to 50 points
 tableView.sectionHeaderHeight = 50

Upvotes: -1

Related Questions