How to change height of grouped UITableView header?

前端 未结 10 643
野性不改
野性不改 2020-12-07 09:22

I know how to change the height of the section headers in the table view. But I am unable to find any solution to change the default spacing before the first section.

相关标签:
10条回答
  • 2020-12-07 10:00

    This worked for me with Swift 4. Modify your UITableView e.g. in viewDidLoad:

    // Remove space between sections.
    tableView.sectionHeaderHeight = 0
    tableView.sectionFooterHeight = 0
    
    // Remove space at top and bottom of tableView.
    tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
    tableView.tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
    
    0 讨论(0)
  • 2020-12-07 10:07

    If you use tableView style grouped, tableView automatically set top and bottom insets. To avoid them and avoid internal insets setting, use delegate methods for header and footer. Never return 0.0 but CGFLOAT_MIN.

    Objective-C

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        // Removes extra padding in Grouped style
        return CGFLOAT_MIN;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        // Removes extra padding in Grouped style
        return CGFLOAT_MIN;
    }
    

    Swift

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        // Removes extra padding in Grouped style
        return CGFloat.leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        // Removes extra padding in Grouped style
        return CGFloat.leastNormalMagnitude
    }
    
    0 讨论(0)
  • 2020-12-07 10:10

    You can try this:

    In the loadView

    _tableView.sectionHeaderHeight = 0;
    

    Then

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

    It should be removed as long as you do not have any objects in the header...

    And if you want some size of the sectionheader, then change only the return value.

    same if you do not get the sectionfooter removed.

    _tableView.sectionFooterHeight = 0;
    

    and

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0;
    }
    

    Well, this works for my problems with the tableview in the iOS7.

    0 讨论(0)
  • 2020-12-07 10:10

    You should remove the code self.tableView.tableHeaderView = [UIView new]; after you add

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return CGFLOAT_MIN;
    }
    
    0 讨论(0)
  • 2020-12-07 10:17

    In swift 2.0

    func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    
            return yourHeight
        }
    
    0 讨论(0)
  • 2020-12-07 10:18

    Return CGFLOAT_MIN instead of 0 for your desired section height.

    Returning 0 causes UITableView to use a default value. This is undocumented behavior. If you return a very small number, you effectively get a zero-height header.

    Swift 3:

     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if section == 0 {
                return CGFloat.leastNormalMagnitude
            }
            return tableView.sectionHeaderHeight
        }
    

    Swift:

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.min
        }
        return tableView.sectionHeaderHeight
    }
    

    Obj-C:

        - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 0)
            return CGFLOAT_MIN;
        return tableView.sectionHeaderHeight;
    }
    
    0 讨论(0)
提交回复
热议问题