Setting tableHeaderView height dynamically
Copied from this post. (Make sure you see it if you're looking for more details)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let headerView = tableView.tableHeaderView {
let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
var headerFrame = headerView.frame
//Comparison necessary to avoid infinite loop
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
tableView.tableHeaderView = headerView
}
}
}
UITableView header dynamic height in run-time
This is how i have approached it
Using tableview
I have created the UI For the header in XIB
Now in the following delegate method
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
}
I Create a UIView for the header and calculate the height based on the content and return the same.
Now i can return the same header view from the following delegate method
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
}
Based on the section i again create a view from xib and return that view from the method.
In my case i needed only one headerview for table so i kept 2 sections and returned the headerview for section one.
How do I set the height of tableHeaderView (UITableView) with autolayout?
You need to use the UIView systemLayoutSizeFittingSize:
method to obtain the minimum bounding size of your header view.
I provide further discussion on using this API in this Q/A:
How to resize superview to fit all subviews with autolayout?
Related Topics
Dtassetproviderservice Could Not Start Dtxconnection with Simulator
How to Give an "Identifier" to a View Controller Within My Storyboard
Nstimer Timerwithtimeinterval: Not Working
It's Possible to Change Push Notification Message Before Display on Device from iOS Side
Setting Selected Image in Tab Bar Controller with Storyboard
Remove Separatorinset on iOS 8 Uitableview for Xcode 6 iPhone Simulator
Get MAC Address of Bluetooth Low Energy Peripheral in iOS
Levenshtein Distance Algorithm Better Than O(N*M)
iOS 10 Error [Access] <Private> When Using Uiimagepickercontroller
How to Read Data Structure from .Plist File into Nsarray
Implement Document Picker in Swift (Ios)
How to Differentiate Between Iphone4 and iPhone 3
What's the Difference Between "Architectures" and "Valid Architectures" in Xcode Build Settings
iOS Aes Encryption - Fail to Encrypt