Sunday, November 1, 2026
HomeiOS Developmentios - How ought to I set constraints to the subviews of...

ios – How ought to I set constraints to the subviews of my tableHeaderView?

[ad_1]

I’ve this viewController:

class CreateSkillGroupViewController: UIViewController {
    
    lazy var headerStack: UIStackView = {
        let stack = UIStackView(body: CGRect(x: 0, y: 0, width: 20, top: 400))
        stack.axis = .vertical
        let titleField = UITextView(body: CGRect(x: 0, y: 0, width: 300, top: 88))
        titleField.backgroundColor = .inexperienced
        titleField.snp.makeConstraints{ (make) in
            make.top.equalTo(50)
        }
        let descriptionField = UITextView(body: CGRect(x: 0, y: 0, width: 300, top: 120))
        descriptionField.snp.makeConstraints{ (make) in
            make.top.equalTo(100)
        }
        let headerImage = UIImageView(picture: UIImage(named: "AppIcon-bw"))
        headerImage.snp.makeConstraints{ (make) in
            make.top.equalTo(300)
            make.width.equalTo(200)
        }
        stack.addArrangedSubview(headerImage)
        stack.addArrangedSubview(titleField)
        stack.addArrangedSubview(descriptionField)
        stack.backgroundColor = .blue
        return stack
    }()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        configureNavigationItem()

        skillsTableView = UITableView(body: .zero, model: .insetGrouped)
        skillsTableView.register(SkillSummaryCell.self)
        skillsTableView.tableHeaderView = headerStack
        view.addSubview(skillsTableView)
        skillsTableView.tableHeaderView?.snp.makeConstraints{ (make) in
            make.high.equalToSuperview()
            make.left.equalToSuperview()
            make.proper.equalToSuperview()
            make.width.equalToSuperview()
            make.top.equalTo(400)
        }
        skillsTableView.snp.makeConstraints{ (make) in
            make.edges.equalToSuperview()
        }
   ...

That is what it creates…

enter image description here

As you possibly can see I take advantage of the lazy var headerStack to setup the tableHeaderView which is a stackView. As you possibly can see all the constraints in that stack view are express quantity sizes. Then within the viewDidLoad, I add the constraints for the tableView itself.

I wish to understand how I might as an illustration, middle the headerImage within the viewController, or within the tableView for that matter or make its width half of the tableView’s width. I can’t set equalToSuperView as a result of the view hasn’t been laid out but. And as soon as its laid out, I can’t entry the stack view subviews to retroactively add constraints to them.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments