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

前端 未结 29 1157
醉梦人生
醉梦人生 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:44

    you can add top + horizontal location constraint between header and tableview, to place it, correctly (if the header itself contains all the necessary internal layout constraints to have a correct frame)

    in the tableViewController viewDidLoad method

        headerView.translatesAutoresizingMaskIntoConstraints = false
    
        tableView.tableHeaderView = headerView
    
        headerView.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true
        headerView.topAnchor.constraint(equalTo: tableView.topAnchor).isActive = true
        headerView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor).isActive = true
    

提交回复
热议问题