Ios 如何在tableview xcode中调整标题大小和更改标题颜色
我正在故事板上使用静态表视图 这些章节都有标题 标题文本的颜色和大小是静态的,我无法更改它 它会导致非常狭窄的标题和黑色文本 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 您需要有一个带有标签的自定义视图,并将其返回到Ios 如何在tableview xcode中调整标题大小和更改标题颜色,ios,swift,Ios,Swift,我正在故事板上使用静态表视图 这些章节都有标题 标题文本的颜色和大小是静态的,我无法更改它 它会导致非常狭窄的标题和黑色文本 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 我怎样才能腾出标题(使高度大一点)并改变文本的颜色 您需要有一个带有标签的自定义视图,并将其返回到UITableView的viewForHeaderInSection的委托方法 func tableView(_ tableView: UITableView, viewForHeaderInSection sectio
UITableView
的viewForHeaderInSection
的委托方法
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Ref:
编辑:
下面是如何做到这一点。基本上,在我上面提到的委托方法中需要一个自定义视图。如果您以前在cellForRow
中成功地创建了自定义UITableViewCell
,那么这一个对您来说应该是小菜一碟
声明一个容器视图,然后在该容器中添加子视图,在本例中,添加一个UILabel
。我总是使用约束,例如:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Let's make the even numbers sections a red background.
// Blue background for odd numbers
let container = UIView()
container.backgroundColor = section % 2 == 0 ? .red : .blue
let titleForHeaderLabel = UILabel()
titleForHeaderLabel.backgroundColor = .white
titleForHeaderLabel.text = "HEADER SECTION: \(section)"
container.addSubview(titleForHeaderLabel)
titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
return container
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80.0
}
然后在func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat
delegate方法中为节提供高度,如下所示:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Let's make the even numbers sections a red background.
// Blue background for odd numbers
let container = UIView()
container.backgroundColor = section % 2 == 0 ? .red : .blue
let titleForHeaderLabel = UILabel()
titleForHeaderLabel.backgroundColor = .white
titleForHeaderLabel.text = "HEADER SECTION: \(section)"
container.addSubview(titleForHeaderLabel)
titleForHeaderLabel.translatesAutoresizingMaskIntoConstraints = false
titleForHeaderLabel.topAnchor.constraint(equalTo: container.topAnchor, constant: 20).isActive = true
titleForHeaderLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -20.0).isActive = true
titleForHeaderLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 20.0).isActive = true
titleForHeaderLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -20.0).isActive = true
return container
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80.0
}
输出:
简单,对吗?:)我希望这有帮助 好吧,让我试试,既然你已经向赏金提出了这个问题;):P