0

I want to set constraint for a label where in the tableview sectionHeader. But when I add constant, I see "Unable to simultaneously satisfy constraints".

so......what should I do.....

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UITableViewHeaderFooterView()
    headerView.backgroundView = {
        let view = UIView()
        view.backgroundColor = .red
        return view
    }()
    headerView.translatesAutoresizingMaskIntoConstraints = false
    
    let headerLabel = UILabel()
    headerView.contentView.addSubview(headerLabel)
    headerLabel.translatesAutoresizingMaskIntoConstraints = false
    headerLabel.text = "\(data[section]) Hello WorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHello"
    headerLabel.numberOfLines = 0
    
    NSLayoutConstraint.activate([
        headerLabel.topAnchor.constraint(equalTo: headerView.contentView.topAnchor, constant: 16),
        headerLabel.bottomAnchor.constraint(equalTo: headerView.contentView.bottomAnchor),
        headerLabel.trailingAnchor.constraint(equalTo: headerView.contentView.trailingAnchor),
        headerLabel.leadingAnchor.constraint(equalTo: headerView.contentView.leadingAnchor)
    ])
    return headerView
}

0 Answers0