Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting tableView header's height in Swift

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):

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:

enter image description here

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!

like image 226
idelara Avatar asked Jul 28 '15 01:07

idelara


People also ask

What is tableView in Swift?

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 ...

How to set the height of the headers in a Tableview?

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.

How to dynamically change Tableview cell height in Swift?

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.

How to create self-sizing Table View headers using UIView?

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

Is it possible to add header and footer view to UITableView?

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.


2 Answers

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 } 
like image 181
Fred Faust Avatar answered Oct 07 '22 09:10

Fred Faust


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!

like image 35
Nick Ivanov Avatar answered Oct 07 '22 08:10

Nick Ivanov