I am trying to set the height of a view that is on top of my prototype cell in a table view controller. I use IB to set it's height (size inspector) and set it to 61 like so (the green view is the 'header' view):
But whenever I run the app, its' height ends up being 568.0
. I have an IBOutlet called testUIView
for the view in my table view controller, and I do: println("testUIView Height->\(testUIView.frame.height)")
and indeed ends up being 568.0
at runtime.
Here is a screenshot showing its' height at runtime:
So my question is: How can I set the view's height so it is 61 at runtime so it indeed looks like my first screenshot (size-wise)?
I tried to set its' height property inside override func viewWillLayoutSubviews()
but it did not let me assign a value to the height testUIView.frame.height = CGFloat(61.0)
.
Any help is appreciated! Thanks in advance!
Cheers!
Overview. Table views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app's content. For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings ...
Headers either in grouped or plain style TableView are no different than any other rows of the UITableView. The setup is actually pretty simple. sectionHeaderHeight and estimatedHeaderHeight need to be set. UITableView.automaticDimension is what enables AutoLayout to calculate the height of each cell at runtime.
Dynamically change TableView Cell height in Swift. To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we’ll need to make use of automatic dimension property. We’ll see this with the help of an sample project.
Let's start by creating a UIView subclass with single label which will act as a HeaderView. We expect this UILabel to be multiline to demonstrate our ability to support self-sizing table view headers. Next, inside your viewDidLoad method add following piece of code to instantiate Component and assign it as a header view
Recently I was playing with support to add header and footer view to UITableView. However, task wasn't as easy as adding UITableViewCell s to the table view. Header and Footer views acts slightly different than regular table view cells.
Here is a solution which uses section header views rather than the actual table header view:
If you'd like to use a header for you UITableView instead you can design another prototype cell in Interface Builder, make a custom class based on a UITableViewCell and assign it to the prototype cell in interface builder on the class inspector.
Then in your controller you're going to use
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
In that function you're actually going to create a reusable cell from your table view but cast as the custom cell you made for the header. You will have access to all of it's properties like a regular UITableViewCell, then you're just going to return the cell's view
return cell.contentView
Another method you're going to use is
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 61.0 }
That one is pretty self explanatory.
Swift 3.0.1
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 61.0 }
Swift 3/Xcode 8:
Add this in viewDidLoad()
:
let HEADER_HEIGHT = 100 tableView.tableHeaderView?.frame.size = CGSize(width: tableView.frame.width, height: CGFloat(HEADER_HEIGHT))
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With