Is it possible to use AutoLayout with UITableView's tableHeaderView?

前端 未结 29 1340
醉梦人生
醉梦人生 2020-11-28 19:51

Since I discovered AutoLayout I use it everywhere, now I\'m trying to use it with a tableHeaderView.

I made a subclass of

29条回答
  •  [愿得一人]
    2020-11-28 20:27

    I saw a lot of methods here doing so much unnecessary stuff, but you don't need that much to use auto layout in the header view. You just have to create you xib file, put your constraints and instantiate it like this:

    func loadHeaderView () {
            guard let headerView = Bundle.main.loadNibNamed("CourseSearchHeader", owner: self, options: nil)?[0] as? UIView else {
                return
            }
            headerView.autoresizingMask = .flexibleWidth
            headerView.translatesAutoresizingMaskIntoConstraints = true
            tableView.tableHeaderView = headerView
        }
    

提交回复
热议问题