【问题标题】:tableHeaderView doesn't properly resizetableHeaderView 没有正确调整大小
【发布时间】:2016-08-19 12:37:46
【问题描述】:

我正在使用 UITableView 和自定义 tableHeaderView

在我的viewDidLayoutSubviews 中,我正在使用这样的正常工作代码:

 let bounds = UIScreen.mainScreen().bounds

    imageSliderVC.view.frame = CGRectMake(0, 0, bounds.width, bounds.width)
    layerView.frame = CGRectMake(0, 0, bounds.width, bounds.width)

    noButton = constructNopeButton()
    yesButton = constructLikedButton()
    buttonOptionsApply()

    nameLbl.frame = CGRectMake(20, layerView.frame.maxY + 20, bounds.width-40, 20)
    nameLbl.makeNameLabelFormat()
    hashtagLbl.frame = CGRectMake(20, nameLbl.frame.maxY + 5, bounds.width-40, layerView.frame.width/3.66)
    hashtagLbl.makeHashtagFormat()

    seperatorLbl.frame = CGRectMake(20, hashtagLbl.frame.maxY + 15, bounds.width-40, 1)

    groupsLbl.frame = CGRectMake(20, seperatorLbl.frame.maxY + 15, 30, 20)
    groupsLbl.makeNameLabelFormat()
    groupsLbl.sizeToFit()
    groupsCountLbl.frame = CGRectMake(groupsLbl.frame.maxX + 5, seperatorLbl.frame.maxY + 15 , 40, 20)
    groupsCountLbl.sizeToFit()

    headerView.frame = CGRectMake(0, 0, bounds.width, groupsCountLbl.frame.maxY)
    table.tableHeaderView = headerView

不使用 headerView 一切都很好地对齐。 该表格与 autoLayout 一起使用 - 将表格固定到所有边缘。

知道为什么会这样吗?尝试使用

        headerView.translatesAutoresizingMaskIntoConstraints = false

它将所有内容与左上边缘对齐。

编辑: 视图的设置就像放在故事板中一样

edit2:截图

它应该是这样的:

这就是它的样子

【问题讨论】:

  • 输出是什么?显示屏幕截图。
  • 你能发布你的 viewForHeaderInSection 方法吗?
  • 我没有使用这个方法。我会看看它。在我的示例中应该是什么样子?
  • 好吧,你的问题是关于标题的,我认为你正在继承一个视图并添加你的自定义行为,但是,也许情况并非如此,所以你可以发布你的 viewController 代码以尝试帮忙?

标签: ios swift uitableview cgrectmake


【解决方案1】:

您可以在 UITableViewDelegate 方法tableView:heightForHeaderInSection: 中设置表hader 视图。当然,使用 NSLayoutConstraint 来定位您的元素 (good tutorial)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多