【发布时间】:2020-12-13 18:37:20
【问题描述】:
我正在向我的 UITableView 添加一个标题视图,并希望向它添加一个具有一些边距的子视图。
class ViewController: UIViewController {
private let tablewView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
tablewView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tablewView)
[
tablewView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
tablewView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
tablewView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tablewView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
].forEach{ $0.isActive = true}
let headerView = UIView(frame: CGRect(x: 0, y: 120, width: 200, height: 100))
headerView.backgroundColor = .blue
let subView = UIView(frame: CGRect(x: 10, y: 10, width: 180, height: 80))
subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
subView.backgroundColor = .yellow
headerView.addSubview(subView)
tablewView.tableHeaderView = headerView
}
}
问题是当标题被调整大小时(当表格视图被布局时),右边距没有被保留。正如您在图像上看到的,缺少右边距:
如果我在没有 UITableView 的情况下使用相同的视图,则按预期保留边距。
这是一个 UIKit 错误吗?有什么解决方法吗?
我知道我可以从这里Is it possible to use AutoLayout with UITableView's tableHeaderView? 尝试 AutoLayout 解决方案,但它们看起来有点老套。毕竟autoresizingMask 应该可以工作。
【问题讨论】:
标签: ios swift uitableview