UITableView headerView 不同大小

Posted

技术标签:

【中文标题】UITableView headerView 不同大小【英文标题】:UITableView headerView different sizes 【发布时间】:2014-12-05 17:13:59 【问题描述】:

我正在尝试在我的 UITableView headerView 上应用大小,但似乎第一个部分标题总是比其余部分 headerViews 小一些。我已经说过 UITableView 的约束,所以应该没问题。第一部分怎么变小了?

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
    return ""


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

    return 20

【问题讨论】:

在我看来您的 UITableView 放置不正确。如果你拉下 UITableView(它会拉下),标题是否正确显示? 我刚刚填充了 tableView 以适应整个 uivieController 并使用了推荐的约束。 问题是其他的。您是否尝试拉下 UITableView 并查看标题是否大小正确,但隐藏在 UINavigationBar 下? 我已经创建了一个 UITableViewController,但我似乎仍然有这个问题 我很好奇您是如何更改表格视图标题的颜色和文本字体的。 【参考方案1】:

使用 Grouped UITableView 时会发生此行为。第一个部分标题的部分标题高度显得较短,因为它上面没有页脚。但是,在第一个节头之后的每个节头,在其正上方都有一个页脚。页脚有一个默认高度,这有助于部分标题的整体高度外观。因此,请设置节页脚高度,以便组节之间的空间具有相同的视觉高度。

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 
    return CGFloat.min

我们使用 CGFloat.min 而不是 0,因为返回 0 会导致页脚返回默认高度。

【讨论】:

斯威夫特 3:CGFloat.leastNormalMagnitude

以上是关于UITableView headerView 不同大小的主要内容,如果未能解决你的问题,请参考以下文章

UITableView headerView 不显示

UITableView headerView 滚动偏移

按钮对 UITableView 的 headerview 没有响应

UITableView HeaderView,FooterView 使用SnapKit布局导致约束异常

第十一篇UITableView headerview下拉放大

iOS UITableview 在有 HeaderView的时候滚动到顶部