2016-08-19 95 views
1

我使用的是UITableView与自定义tableHeaderViewtableHeaderView不能正确调整

在我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:截图

它应该是这个样子:

enter image description here

这就是它看起来像

enter image description here

+0

什么是输出?显示截图。 – Santosh

+0

你可以发布你的viewForHeaderInSection方法吗? –

+0

我没有使用这种方法。我会看看它。在我的例子中它应该是什么样子? – JVS

回答

0

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