ios - 缩进表格中的标题
问题描述
我有一张有两个部分的桌子。标题部分的属性textAligment = .right
。但它离手机的边框太近了。如何从靠近中心的边界缩进?
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = .black
header.textLabel?.textAlignment = .right
}
解决方案
这是一个非常简单的例子......
- 在情节提要中添加一个
UITableViewController
。 - 将其自定义类分配给
SectionTableViewController
(来自下面的代码)。 - 使用默认单元格。
- 给单元格一个标识符“DefCell”
运行应用程序。这是你应该看到的:
我已经限制标签使用contentView.layoutMarginsGuide
并将尾随常量设置为-32
class MySectionHeaderView: UITableViewHeaderFooterView {
let myLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.textColor = .black
v.textAlignment = .right
// during development, give it a background color to make it easy to see the frame
//v.backgroundColor = .cyan
return v
}()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.addSubview(myLabel)
// use layout margins guide
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
myLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
myLabel.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
myLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
// set the constant to provide more trailing space as desired
// Note: must be a negative value
myLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -32.0),
])
}
}
class SectionTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(MySectionHeaderView.self, forHeaderFooterViewReuseIdentifier: "MyHeader")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "DefCell", for: indexPath)
c.textLabel?.text = "Section: \(indexPath.section) Row: \(indexPath.row)"
return c
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let v = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MyHeader") as? MySectionHeaderView else {
fatalError("Could not dequeue MySectionHeaderView!")
}
v.myLabel.text = "Section \(section)"
return v
}
}
您会在 in 的声明中注意到这些myLabel
行MySectionHeaderView
:
// during development, give it a background color to make it easy to see the frame
//v.backgroundColor = .cyan
这是标签具有背景颜色时的外观,因此您可以轻松查看标签的布局方式:
推荐阅读
- extjs - 我正在尝试增加 extjs 中网格列标题的边框权重,但它不会增加超过 1px 有没有其他方法可以增加?
- visual-studio-code - VSCode:使用正则表达式查找和替换并保留周围的上下文(未捕获的部分)
- python - FastAPI - 向 TestClient 添加路由前缀
- jsf - JSF 2.3 使用来自休息服务的@PushContext
- flutter - 如何使用 MongoDB 数据库实现 Google Sign in Flutter 应用
- xcode - 编译C编译器识别源文件“CMakeCCompilerId.c”失败。编译器:/Applications/Xcode.app/......./usr/bin/cc
- django - Djongo 和 Django
- html - 关于简单 Bootstrap 3.4.1 表中缺失列的任何想法?
- javascript - FetchError:无法验证第一个证书,但我添加了rejectUnauthorized:false
- string - Powershell,使用(空格分隔)字符串作为程序的参数