How to set table header height in UITableView?

I looked at Apple's UITableView class documents and delegated the link, but couldn't find a way to explicitly set the table header height.

I set the table cell height using the following delegate:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

and set the height of the section header / footer using the following delegates.

 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 

Can someone help me set the height of the header / footer of the table?

Thank.

+68
iphone uitableview
Apr 7 2018-11-11T00:
source share
16 answers

Just set the frame tableHeaderView property.

+59
Apr 7 '11 at 12:40
source share

I found a good hack. Add the line below after changing the frame itself

 self.tableView.tableHeaderView = self.tableView.tableHeaderView; 

The trick (I think) is that a UITableView caches the height (actual frame) when you assign a view to the tableHeaderView property. The above line just sets the height again.

+58
Sep 28 '12 at 10:11
source share
 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() sizeHeaderToFit() } private func sizeHeaderToFit() { let headerView = tableView.tableHeaderView! headerView.setNeedsLayout() headerView.layoutIfNeeded() let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height var frame = headerView.frame frame.size.height = height headerView.frame = frame tableView.tableHeaderView = headerView } 

More information can be found here.

+35
Jan 05 '16 at 10:46 on
source share

If you still need to try setting the property

 self.tableView.tableHeaderView 

If you calculate the level you need and set a new view for tableHeaderView :

 CGRect frame = self.tableView.tableHeaderView.frame; frame.size.height = newHeight; self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:frame]; 

It should work.

+9
Sep 17 '12 at 14:17
source share

It only works with me if you first set the footer / column of the nil table:

 self.footer = self.searchTableView.tableFooterView; CGRect frame = self.footer.frame; frame.size.height = 200; self.footer.frame = frame; self.searchTableView.tableFooterView = nil; self.searchTableView.tableFooterView = self.footer; 

Make sure self.footer is a strong reference to prevent freeing the footer view

+6
Sep 11 '14 at 13:06 on
source share

Swift 4 - you can control the height with HEIGHT_VIEW, just add this encoding, it works

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() let HEIGHT_VIEW = 60 tableView.tableFooterView?.frame.size = CGSize(width: tblView.frame.width, height: CGFloat(HEIGHT_VIEW)) tableView.tableHeaderView?.frame.size = CGSize(width:tblView.frame.width, height: CGFloat(HEIGHT_VIEW)) } 
+4
Nov 28 '17 at 10:05
source share

Just create a footer viewer using the UIView(frame:_) constructor, if you are using an xib file for FooterView, create a view from xib and add a subView as a view to the shell. then assign a wrapper to tableView.tableFooterView = fixWrapper .

  let fixWrapper = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 54)) // dont remove let footer = UIView.viewFromNib("YourViewXibFileName") as! YourViewClassName fixWrapper.addSubview(footer) tableView.tableFooterView = fixWrapper tableFootterCostView = footer 

It works great for me! The point is to create a footer with a constructor (frame:_) . Even if you create UIView () and assign a frame property, it may not work.

+2
May 7 '16 at 12:51
source share

If you add a table header view in IB, set the frame for this view in IB on tab 5 (size inspector)

+1
Sep 13 '17 at 1:25
source share

If you use XIB for the main tableView header, you can set XIB as an arbitrary shape, set the desired height and cancel the automatic resizing of the upper, lower blocks and the upper, lower arrows. Only horizontal parts will be selected. Vertical will be canceled, as I mentioned above.,

+1
Oct 23 '18 at 6:27
source share

You can create a UIView with the desired height (the width must match the width of the UITableView), and inside it you can place the UIImageView with the image of the desired size: they will not stretch automatically.

You can also set the field above and below the internal UIImageView by increasing the height of the container view.

In addition, you can assign a translation transformation to place the image, for example, in the middle of the container header view.

+1
Feb 06 '19 at 12:59
source share
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { } 

or you can use like this also

 tableView.estimatedSectionHeaderHeight 
0
May 08 '18 at 9:24
source share

In Xcode 10, you can set the header and footer of the section height on the Size Inspector tab

Size inspector header

0
May 16 '19 at 19:41
source share

If you programmatically set tableHeaderView , then just set it inside viewDidLayoutSubviews .

  override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() setupTableViewHeader() } private func setupTableViewHeader() { // Something you do to set it up programatically... tableView.tableHeaderView = MyHeaderView.instanceFromNib() } 

If you did not install it programmatically, you need to do the same thing @Kris answered based on this link

  override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() sizeHeaderToFit() } private func sizeHeaderToFit() { if let headerView = tableView.tableHeaderView { headerView.setNeedsLayout() headerView.layoutIfNeeded() let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height var frame = headerView.frame frame.size.height = height headerView.frame = frame tableView.tableHeaderView = headerView } } 
0
Aug 07 '19 at 2:36
source share

Use the table view presentation property:

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 35.0; } 

thank

-one
Apr 07 2018-11-12T00:
source share

If you changed the height of the tableView headerView, just use this code to update the tableView:

ObjC:

 [tableView beginUpdates]; tableView.endUpdates() 

Swift:

 tableView.beginUpdates() tableView.endUpdates() 
-one
Feb 28 '18 at 7:15
source share

With autolayout, you can do something like:

 tableView.sectionHeaderHeight = UITableViewAutomaticDimension tableView.estimatedSectionHeaderHeight = <your-header-height> 

or if your headers are of different heights, go ahead and do:

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return <your-header-height> } 
-one
Apr 09 '18 at 10:17
source share



All Articles