I want to indicate occasional lengthy texts in a UIButton within the part header of my UITableView. The next easy instance code exhibits the difficulty:
import UIKit
import SnapKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView(body: .zero, type: .plain)
let sections = ["Lorem","Lorem Ipsum","Lorem Ipsum is simply dummy text.","Lorem Ipsum is simply dummy text of the printing and typesetting industry."]
override func viewDidLoad() {
tremendous.viewDidLoad()
tableView.register(SectionHeader.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = UITableView.automaticDimension
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in tableView: UITableView) -> Int {
sections.rely
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(type: .subtitle, reuseIdentifier: "cell")
cell.textLabel?.textual content = "Index (indexPath.row)"
cell.detailTextLabel?.textual content = "Part (indexPath.part)"
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection part: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! SectionHeader
let attr = NSAttributedString(string: sections[section], attributes: [.font: UIFont.preferredFont(forTextStyle: .extraLargeTitle)])
header.sectionTitle.setAttributedTitle(attr, for: .regular)
header.setNeedsLayout()
header.layoutIfNeeded()
return header
}
}
class SectionHeader: UITableViewHeaderFooterView {
let sectionTitle = UIButton()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
override init(reuseIdentifier: String?) {
tremendous.init(reuseIdentifier: reuseIdentifier)
sectionTitle.contentHorizontalAlignment = .left
contentView.addSubview(sectionTitle)
let padding = 10.0
sectionTitle.snp.makeConstraints { make in
make.left.equalToSuperview().inset(padding)
make.proper.equalToSuperview().inset(padding)
make.prime.equalToSuperview().inset(padding)
make.backside.equalToSuperview().inset(padding)
if let t = sectionTitle.titleLabel {
t.numberOfLines = 0
t.lineBreakMode = .byWordWrapping
t.adjustsFontForContentSizeCategory = true
make.top.equalTo(t)
}
}
}
}
The precise structure is okay and the textual content exhibits accurately with longer textual content being wrapped:
Nonetheless, Xcode retains printing the beneath UIViewAlertForUnsatisfiableConstraints Unable to concurrently fulfill constraints warnings:
Unable to concurrently fulfill constraints.
In all probability at the least one of many constraints within the following record is one you do not need.
Do that:
(1) have a look at every constraint and take a look at to determine which you do not anticipate;
(2) discover the code that added the undesirable constraint or constraints and repair it.
(
"[email protected]#73 UIButton:0x101b1cbd0.left == _UITableViewHeaderFooterContentView:0x101b1e130.left + 10.0>",
"[email protected]#74 UIButton:0x101b1cbd0.proper == _UITableViewHeaderFooterContentView:0x101b1e130.proper - 10.0>",
"" " "" """ " "" """ " "" """ " "" """ " "" """ 0x600002132760> 0x600002132710> 0x600002127570> 0x600002127520> 0x60000210e990> 0x60000210e760>


