How to set the height of table header in UITableView?

前端 未结 17 1349
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 22:00

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

相关标签:
17条回答
  • 2020-12-02 22:39

    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.

    0 讨论(0)
  • 2020-12-02 22:39

    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>
    }
    
    0 讨论(0)
  • 2020-12-02 22:42

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

    0 讨论(0)
  • 2020-12-02 22:44

    @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;
    }
    
    0 讨论(0)
  • 2020-12-02 22:44

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

    0 讨论(0)
  • 2020-12-02 22:45

    Just set the frame property of the tableHeaderView.

    0 讨论(0)
提交回复
热议问题