2015-05-15 34 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,它将开始伸展。