Ios 具有动态高度的viewForHeader?

Ios 具有动态高度的viewForHeader?,ios,uitableview,Ios,Uitableview,我返回了一个动态高度的视图,用于表视图中的视图forheader。我有2个部分。但是我得到的视图是0部分中标题部分1的视图,没有视图,出现空白。下面是代码 tblList.estimatedSectionHeaderHeight = 100 tblList.sectionHeaderHeight = UITableViewAutomaticDimension func numberOfSections(in tableView: UITable

我返回了一个动态高度的视图,用于
表视图中的
视图forheader
。我有2个部分。但是我得到的视图是0部分中标题部分1的视图,没有视图,出现空白。下面是代码

        tblList.estimatedSectionHeaderHeight = 100
        tblList.sectionHeaderHeight = UITableViewAutomaticDimension


        func numberOfSections(in tableView: UITableView) -> Int {
            return 2
        }

       func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        print("Section: \(section)")
            let view = UIView()
            let lbl = UILabel()
           view.backgroundColor = UIColor.LGColor()

            view.translatesAutoresizingMaskIntoConstraints  = false
            lbl.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(lbl)
            lbl.font = UIFont.subHeadline

            view.addConstraint(NSLayoutConstraint(item: lbl, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 3))

            view.addConstraint(NSLayoutConstraint(item: lbl, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 10))

            view.addConstraint(NSLayoutConstraint(item: lbl, attribute: .trailing, relatedBy: .lessThanOrEqual, toItem: view, attribute: .trailing, multiplier: 1, constant: -10))

            view.addConstraint(NSLayoutConstraint(item: lbl, attribute: .height, relatedBy:.greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 21))

            view.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: lbl, attribute: .bottom, multiplier: 1, constant: 3))


   view.addConstraint(NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: tableView.frame.size.width))

        if section == 0 {
            view.backgroundColor = .red
        } else {
            view.backgroundColor = .yellow
        }
        return view
}

这是因为您设置了
view.translatesAutoResizengMaskintoConstraints=false
,注释该行并检查是否可以同时看到这两个部分这是因为您为标题视图设置了
translatesAutoResizengMaskintoConstraints=false
,请删除该行

它删除了在
TableView
中创建的约束,并且两个视图具有原点
点{0,0}