How to set the height of table header in UITableView?

前端 未结 17 1348
没有蜡笔的小新
没有蜡笔的小新 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:48

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

    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.

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

    Use table view default property :

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

    Thanks

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

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

    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
            }
        }
    
    
    0 讨论(0)
提交回复
热议问题