我有一个“普通”风格的 UITableView。我将 View 设置为 TableView 的 tableViewHeader
。该表还显示了右侧下方的部分索引。
我的问题是弄清楚如何插入标题 View 的左右边缘以考虑安全区域插入(如果在 iPhone X(横向)上运行)和表格 View 的部分索引(如果有的话)。
我创建了一个简单的测试应用程序,它添加了一些虚拟行、一个节标题和节索引。
这是我使用 UILabel 创建简单标题 View 的代码。我的真实应用不会使用标签,而是使用自定义 View 。
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 30)
label.backgroundColor = .green
label.text = "This is a really long Table Header label to make sure it is working properly."
label.sizeToFit()
tableView.tableHeaderView = label
没有特别尝试修复左右边缘,在iPhone X模拟器中的结果如下:
肖像:
风景:
请注意,无需任何额外的努力,单元格和部分标题会获得所需的插图,但标题 View 不会。
我希望标题 View 的左边缘与节标题的左边缘和单元格对齐。
我希望标题 View 的右边缘在与节索引的左边缘相交处停止。请注意,纵向图片似乎已经这样做了,但是如果您仔细观察,您可以知道标题 View 一直到表格 View 的右边缘。您看不到椭圆的第三个 .
,您几乎看不到节标题 View 后面的绿色。
我所做的一个尝试是将以下内容添加到我的表格 View Controller 中:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let header = tableView.tableHeaderView {
var insets = header.layoutMargins
insets.left = tableView.layoutMargins.left
insets.right = tableView.layoutMargins.right
header.layoutMargins = insets
}
}
该代码无效。
我应该设置哪些属性来确保标题 View 的左右边缘根据需要缩进?是否有应应用的限制条件?
请注意,我在代码中做所有事情。所以请不要发布任何需要 Storyboard或 xib 文件的解决方案。欢迎使用 Swift 或 Objective-C 的答案。
对于想要复制此内容的任何人,请创建一个新的单 View 项目。调整主 Storyboard以使用 UITableViewController 而不是计划 UIViewController 并为 ViewController
使用以下内容:
import UIKit
class ViewController: UITableViewController {
// MARK: - UITableViewController methods
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Row \(indexPath.row)"
cell.accessoryType = .disclosureIndicator
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section Header"
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
let coll = UILocalizedIndexedCollation.current()
return coll.sectionIndexTitles
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return index
}
// MARK: - UIViewController methods
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionIndexMinimumDisplayRowCount = 1
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 30)
label.backgroundColor = .green
label.text = "This is a really long Table Header label to make sure it is working properly."
label.sizeToFit()
tableView.tableHeaderView = label
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let header = tableView.tableHeaderView {
var insets = header.layoutMargins
insets.left = tableView.layoutMargins.left
insets.right = tableView.layoutMargins.right
header.layoutMargins = insets
}
}
}
对于没有节索引的 UITableViews:
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
对于带有节索引的 UITableView:
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
label.trailingAnchor.constraint(equalTo: tableView.layoutMarginsGuide.trailingAnchor)
])
关于ios - 如何调整 UITableView tableHeaderView 的左右插入或边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696297/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) | Powered by Discuz! X3.4 |