ios – tableHeaderView is overlapping cells when including customized view to subview of container view

0
13


I’m presently utilizing a UIViewController and including a UITableView to the view. With this tableView I’m including a UIView known as containerView to its tableHeaderView. I set the peak of the container view after which including a second UIView to its subview, that’s pinned to the underside of the containerView.

Once I add it to the header view the cells are being overlapped. What’s odd although is that if I do not add the subview to the container view the headerView isn’t being overlapped by the cells, it is just occurring when I’m including the second view as a subview to the container view.

class ViewController: UIViewController {

    personal var containerView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.alpha = 0.7
        view.backgroundColor = .crimson
        return view
    }()

    personal var bottomView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .blue
        return view
    }()

    personal(set) lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableView
    }()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        view.addSubview(tableView)

        containerView.addSubview(bottomView)
        tableView.tableHeaderView = containerView

        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            containerView.topAnchor.constraint(equalTo: tableView.topAnchor),
            containerView.heightAnchor.constraint(equalToConstant: 214),
            containerView.widthAnchor.constraint(equalToConstant: view.frame.size.width),

            bottomView.topAnchor.constraint(equalTo: containerView.bottomAnchor),
            bottomView.heightAnchor.constraint(equalToConstant: 114),
            bottomView.widthAnchor.constraint(equalToConstant: view.frame.size.width),
        ])
    }

    override func viewDidLayoutSubviews() {
        tremendous.viewDidLayoutSubviews()
        tableView.contentInset = UIEdgeInsets(prime: -view.safeAreaInsets.prime, left: 0, backside: 0, proper: 0)
        tableView.tableHeaderView?.autoresizingMask = []
        tableView.tableHeaderView?.layoutIfNeeded()
    }
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(true)
    }
} 

enter image description here

LEAVE A REPLY

Please enter your comment!
Please enter your name here