2016-08-19 93 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)。