[英]autoresizingMask not working as expected for UITableView.tableHeaderView's subview
I'm adding a header view to my UITableView and want to add a subview to it having some margins.我正在向我的 UITableView 添加一个 header 视图,并希望向它添加一个具有一些边距的子视图。
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
}
}
The problem is that the right margin isn't preserved when the header is resized (when the table view is laid out).问题是当 header 被调整大小时(当表格视图被布局时),右边距没有被保留。 As you can see on the image, the right margin is missing:
正如您在图像上看到的,缺少右边距:
If I'm using the same view without UITableView, then the margin is preserved as expected.如果我使用没有 UITableView 的相同视图,则按预期保留边距。
Is it a UIKit bug?它是 UIKit 错误吗? Are there any workarounds?
有什么解决方法吗?
I know that I can try AutoLayout solutions from here Is it possible to use AutoLayout with UITableView's tableHeaderView?我知道我可以从这里尝试 AutoLayout 解决方案是否可以将 AutoLayout 与 UITableView 的 tableHeaderView 一起使用? but they're looking a bit hacky.
但他们看起来有点老套。
autoresizingMask
is supposed to work, after all.毕竟,
autoresizingMask
应该可以工作。
In Cocoa programming as in comedy, timing is everything.在 Cocoa 编程中,就像在喜剧中一样,时间就是一切。
Add the subview in a one-time implementation of viewDidLayoutSubviews
and all will be well.在
viewDidLayoutSubviews
的一次性实现中添加子视图,一切都会好起来的。 The subview will appear correctly, and will continue working if the table view is resized (eg due to rotation of the interface).子视图将正确显示,并且如果调整表格视图的大小(例如由于界面的旋转)将继续工作。
So, cut these four lines:所以,剪掉这四行:
let subView = UIView(frame: CGRect(x: 10, y: 10, width: 180, height: 80))
subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
subView.backgroundColor = .yellow
headerView.addSubview(subView)
And instead:相反:
var didLayout = false
override func viewDidLayoutSubviews() {
guard !didLayout else { return }
didLayout.toggle()
if let h = tablewView.tableHeaderView {
let subView = UIView(frame: h.bounds.insetBy(dx: 10, dy: 10))
subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
subView.backgroundColor = .yellow
h.addSubview(subView)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.