ios - 对于自定义 UIView() 类,不会自动调整 UITableView 标题部分的高度

标签 ios swift uitableview ios-autolayout uitableviewsectionheader

我有 UIView() 类,我在其中以编程方式添加标签,并且还给出了约束以根据内容自动调整 View 高度。我已将此类用作 UItableView 部分的 HeaderView。但这里的问题是此 View 的高度未根据其内容进行相应调整。

这是我的那个自定义 View 的代码。

class DynamicHeaderView: UIView {

override func draw(_ rect: CGRect) {
    let headerLabel = UILabel()
    headerLabel.numberOfLines = 0
    headerLabel.sizeToFit()
    headerLabel.text = "This is header view. It is dynamicaaly growing text and will automaticaly get adjusted to it"
    self.backgroundColor = .green
    headerLabel.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(headerLabel)

    self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 16))

    self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -16))

    self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))

    self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: -10))
} }

我在 viewController 中编写的代码,

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.countriesTable.sectionHeaderHeight = UITableView.automaticDimension;
    self.countriesTable.estimatedSectionHeaderHeight = 25 }

 func tableView(_ tableView: UITableView,
               viewForHeaderInSection section: Int) -> UIView? {
    let headerView = DynamicHeaderView()
    return headerView
}

高度始终保持在我在 viewDidLoad() 函数中给出的估计标题高度为 25。

最佳答案

你需要继承UITableViewHeaderFooterView,然后注册tableView,最后实现viewForHeaderInSection

class DynamicHeaderView: UITableViewHeaderFooterView {

    let headerLabel = UILabel()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        headerLabel.numberOfLines = 0
     //   headerLabel.sizeToFit()
        headerLabel.text = "This is header view. It is dynamicaaly growing text and will automaticaly get adjusted to it"
        self.backgroundColor = .green
        headerLabel.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(headerLabel)

        headerLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: .vertical)

        self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 16))

        self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -16))

        self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))

        self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 10))
    }



    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//

viewDidLoad

 tableView.register(DynamicHeaderView.self, forHeaderFooterViewReuseIdentifier: "celld")  

 tableView.estimatedSectionHeaderHeight = 50 

 tableView.sectionHeaderHeight = UITableView.automaticDimension

//

func tableView(_ tableView: UITableView,
               viewForHeaderInSection section: Int) -> UIView? {

    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "celld") as! DynamicHeaderView


    headerView.headerLabel.text = "heightForHeaderInSectionheightForHeaderInSectionheightForHeaderInSectionheightForHeaderInSectionheightForHeaderInSectionheightForHeaderInSection"

    return headerView
}

关于ios - 对于自定义 UIView() 类,不会自动调整 UITableView 标题部分的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54378520/

相关文章:

ios - UISwitch - UITableView

xcode - 是否可以制作 "Alphabetical scroll bar - alike"进行搜索?

ios - tableview 在使用 xib 单元格时发现 nil

使用关联类型和 Self 的 Swift 协议(protocol)?

swift - 使用 NSTask 运行多个终端命令

ios - 仅将圆角添加到 UITableView 的顶部?

iOS14 navigationItem.largeTitleDisplayMode = .always 不工作

ios - 在iOS8上实现iOS9+协议(protocol)

ios - 创建存档以在 AppStore 中发布

ios - 如何在 iOS 应用程序项目中正确嵌入第 3 方 .dylib 文件以供 App Store 发布?