简体   繁体   English

对于 UITableView.tableHeaderView 的子视图,autoresizingMask 未按预期工作

[英]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.

相关问题 UISearchBar未设置为UITableView.tableHeaderView - UISearchBar not being set to UITableView.tableHeaderView 如何在UITableView.tableHeaderView中居中显示文本? - How to Center Text in UITableView.tableHeaderView? UITableView tableHeaderView用户交互无法正常工作 - UITableView tableHeaderView user interaction not working 当与添加到子视图的UITableView一起使用时,prefersLargeTitles无法正常工作 - prefersLargeTitles not working as expected when used with a UITableView added to a subview 我可以更改uitableview的tableHeaderView的高度吗? - Can i change the height of a uitableview's tableHeaderView? 是否可以将 AutoLayout 与 UITableView 的 tableHeaderView 一起使用? - Is it possible to use AutoLayout with UITableView's tableHeaderView? UITableView的子视图未显示 - UITableView's subview not showing 如何像 UITableView 的 tableHeaderView 一样在 UICollectionView 中添加 HeaderView - How to add HeaderView in UICollectionView like UITableView's tableHeaderView 试图使UITableView的tableHeaderView超出表范围 - Trying to make UITableView's tableHeaderView outside of table bounds 是否可以仅基于自动布局来定义UITableView的tableHeaderView的高度? - Is is possible to define the height of a UITableView's tableHeaderView purely based on Auto layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM