Paresh Masani
Paresh Masani

Reputation: 7504

How to set the height of table header in UITableView?

I have gone through Apple docs about UITableView class and delegate reference but couldn't find the way to set the table header height explicitly.

I set Table cell height using following delegate:

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

and set section header/footer height using following delegates.

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

Could anyone please help me to set the table header/footer height?

Thanks.

Upvotes: 81

Views: 129588

Answers (17)

Meilbn
Meilbn

Reputation: 592

If you changed height of tableView's headerView, just reset headerView's frame, then, reset headerView of tableView:

self.headerView.frame = newFrame;
self.tableView.tableHeaderView = self.headerView;

Upvotes: -1

Yogesh Patel
Yogesh Patel

Reputation: 2017

@kris answer is helpful for me anyone want it in Objective-C.

Here is the code

-(void)viewDidLayoutSubviews{
     [super viewDidLayoutSubviews];
     [self sizeHeaderToFit];
}

-(void)sizeHeaderToFit{
     UIView *headerView = self.tableView.tableHeaderView;
     [headerView setNeedsLayout];
     [headerView layoutIfNeeded];
     CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
     CGRect frame = headerView.frame;
     frame.size.height = height;
     headerView.frame = frame;
     self.tableView.tableHeaderView = headerView;
}

Upvotes: 2

meow2x
meow2x

Reputation: 2124

If you programatically set the 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 didn't set it programatically, you need to do similar to what @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
        }
    }

Upvotes: 2

NinjaDeveloper
NinjaDeveloper

Reputation: 1712

In Xcode 10 you can set header and footer of section hight from "Size Inspector" tab

Size Inspector header

Upvotes: -1

Gabriel
Gabriel

Reputation: 1449

You can create a UIView with the desired height (the width should be that of the UITableView), and inside it you can place a UIImageView with the picture of the proper dimensions: they won't stretch automatically.

You can also give margin above and below the inner UIImageView, by giving a higher height to the container view.

Additionally, you can assign a Translation transform in order to place the image in the middle of its container header view, for example.

Upvotes: 1

Kris
Kris

Reputation: 1556

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 details can be found here

Upvotes: 44

Krunal Patel
Krunal Patel

Reputation: 1691

Swift 4 - you can manage height with HEIGHT_VIEW,Just add this cods, Its working

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

Upvotes: 4

Talip Böke
Talip Böke

Reputation: 11

If you are using XIB for tableView's main headerView you can set XIB as a freeform set the Height as you want and unclick Autoresizing's top,bottom blocks and upper,lower arrows.Only horizontal pieces will be selected.Vertical will be unselected as I mentioned above.

Upvotes: 1

Mnsd
Mnsd

Reputation: 35

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
}

or you can use like this also

tableView.estimatedSectionHeaderHeight 

Upvotes: -3

Matti
Matti

Reputation: 484

With autolayout you could do something like:

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

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

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return <your-header-height>
}

Upvotes: -2

Yllow
Yllow

Reputation: 326

If add a view as table header view in IB, set the frame of that view in IB in Tab 5(size inspector)

Upvotes: 2

Sergey Sahakyan
Sergey Sahakyan

Reputation: 739

Just create Footer Wrapper View using constructor UIView(frame:_) then if you are using xib file for FooterView, create view from xib and add as subView to wrapper view. then assign 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 perfectly for me! the point is to create footer view with constructor (frame:_). Even though you create UIView() and assign frame property it may not work.

Upvotes: 2

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7123

It works with me only if I set the footer/header of the tableview to nil first:

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 that self.footer is a strong reference to prevent the footer view from being deallocated

Upvotes: 7

Cristian Bica
Cristian Bica

Reputation: 4117

I found a nice hack. Add the below line after modifying the frame propery

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

The trick is (I think) that the UITableView is caching the height (the frame actually) when you assign the view to the tableHeaderView property. The above line just assigns the height again.

Upvotes: 62

Fabio
Fabio

Reputation: 251

In case you still need it, have you tried to set the property

self.tableView.tableHeaderView 

If you calculate the heigh 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.

Upvotes: 10

Mitesh Khatri
Mitesh Khatri

Reputation: 3955

Use table view default property :

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

Thanks

Upvotes: -3

Roger
Roger

Reputation: 15813

Just set the frame property of the tableHeaderView.

Upvotes: 68

Related Questions