How to set the height of table header in UITableView?

前端 未结 17 1346
没有蜡笔的小新
没有蜡笔的小新 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:24
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    }
    

    or you can use like this also

    tableView.estimatedSectionHeaderHeight 
    
    0 讨论(0)
  • 2020-12-02 22:25
    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

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

    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.

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

    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

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

    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.

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

    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.

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