【发布时间】:2016-05-02 16:16:04
【问题描述】:
我正在尝试自定义 UITableView 部分中的标题。我在故事板中找不到这样做,所以我开始添加 UIView 和 UILabel。
这是我的代码:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0,0, tableView.frame.size.width, 60))
headerView.backgroundColor = UIColor.cyanColor()
headerView.layer.borderColor = UIColor.whiteColor().CGColor
headerView.layer.borderWidth = 1.0;
let headerLabel = UILabel(frame: CGRectMake(5,2, tableView.frame.size.width - 5, 30))
headerLabel.text = sectionTitle (this is a variable)
headerLabel.textAlignment = NSTextAlignment.Center
headerLabel.font = UIFont(name: "AvenirNext", size: 18)
headerLabel.textAlignment = NSTextAlignment.Center;
headerView.addSubview(headerLabel)
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
我正在寻找一种方法来垂直和水平定位视图中心的 UILabel。我该怎么做?每当我更改字体和大小时,它总是会改变吗?
另外,无论如何我可以在情节提要中做到这一点吗?以编程方式制作标题真的很难。
【问题讨论】:
-
stackoverflow.com/questions/15611374/… 您应该实现该方法并在情节提要中创建自定义视图。在该方法中,加载您的自定义视图并返回它。
-
完美的演示,但是如果我想在向上滚动时修复位置视图标题,可以吗?
标签: ios swift uitableview