2015-05-15 37 views
2

我有一個自定義標題,我從一個nib文件製作的tableview。UITableView的HeaderFooterView不根據尺寸等級寬度拉伸

nib文件的大小等級爲Any x Any,但頭部寬度不適合iPhone 6 & iPhone 6Plus。

下面是如何使HeaderFooterView

let headerCell = UINib(nibName: "TeamCheckHeaderTableViewCell", bundle: nil) 
tblTeamCheck.registerNib(headerCell, forHeaderFooterViewReuseIdentifier: TeamCheckHeaderTableViewCell.description()) 

viewForHeaderInSection

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let headerCell = tblTeamCheck.dequeueReusableHeaderFooterViewWithIdentifier(TeamCheckHeaderTableViewCell.description()) as! TeamCheckHeaderTableViewCell 

    return headerCell 
} 

這裏是它的外觀上iPhone 5

enter image description here

這裏是如何看起來對iPhone 6

enter image description here

與6Plus同樣,這似乎是HeaderFooterView的寬度是恆定的320

+0

您是否在viewDidLoad中創建headerCell?如果是這樣,請在viewWillAppear中移動它並調用其layoutIfNeeded方法 –

回答

0

嘗試添加headerCell.layoutIfNeeded()func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

0

找到了解決辦法,但可能不是最好的 - 仍處於打開狀態更好的解決方案

我在.xib文件中將頭部單元的寬度硬編碼爲414 - 這是iPhone 6 Plus的寬度。

然後以編程方式計算中心標籤的位置,並設置約束條件使其居中,而不管它是哪個iPhone屏幕。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let headerCell = tblTeamCheck.dequeueReusableHeaderFooterViewWithIdentifier(TeamCheckHeaderTableViewCell.description()) as! TeamCheckHeaderTableViewCell 

    //Calculate the diff and set the new constraint 
    let tableViewWidth = tableView.frame.size.width 
    let headerCellWidth : CGFloat = headerCell.frame.size.width 
    let diff = headerCellWidth - tableViewWidth 
    headerCell.centerConstraint.constant = diff/2 

    return headerCell 
} 

我創建的約束作爲一個變量在TeamCheckHeaderTableViewCell

@IBOutlet weak var centerConstraint: NSLayoutConstraint! 
0

檢查類類型的項目您的廈門國際銀行內。你可能已經使用了UITableViewCell。將它替換爲UIView,它將開始伸展。