I need to show occasional long texts in a UIButton in the section header of my UITableView. The following simple example code shows the issue:
import UIKit
import SnapKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView(frame: .zero, style: .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() {
super.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.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = "Index \(indexPath.row)"
cell.detailTextLabel?.text = "Section \(indexPath.section)"
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: 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: .normal)
header.setNeedsLayout()
header.layoutIfNeeded()
return header
}
}
class SectionHeader: UITableViewHeaderFooterView {
let sectionTitle = UIButton()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
sectionTitle.contentHorizontalAlignment = .left
contentView.addSubview(sectionTitle)
let padding = 10.0
sectionTitle.snp.makeConstraints { make in
make.left.equalToSuperview().inset(padding)
make.right.equalToSuperview().inset(padding)
make.top.equalToSuperview().inset(padding)
make.bottom.equalToSuperview().inset(padding)
if let t = sectionTitle.titleLabel {
t.numberOfLines = 0
t.lineBreakMode = .byWordWrapping
t.adjustsFontForContentSizeCategory = true
make.height.equalTo(t)
}
}
}
}
The actual layout is okay and the text shows correctly with longer text being wrapped:
However, Xcode keeps printing the below UIViewAlertForUnsatisfiableConstraints Unable to simultaneously satisfy constraints warnings:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#73 UIButton:0x101b1cbd0.left == _UITableViewHeaderFooterContentView:0x101b1e130.left + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101b1cbd0.right == _UITableViewHeaderFooterContentView:0x101b1e130.right - 10.0>",
"<NSLayoutConstraint:0x60000210e760 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x101b1e130.width == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101b1cbd0.right == _UITableViewHeaderFooterContentView:0x101b1e130.right - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#75 UIButton:0x101b1cbd0.top == _UITableViewHeaderFooterContentView:0x101b1e130.top + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101b1cbd0.bottom == _UITableViewHeaderFooterContentView:0x101b1e130.bottom - 10.0>",
"<NSLayoutConstraint:0x60000210e990 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x101b1e130.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101b1cbd0.bottom == _UITableViewHeaderFooterContentView:0x101b1e130.bottom - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#73 UIButton:0x101e180e0.left == _UITableViewHeaderFooterContentView:0x101e18990.left + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101e180e0.right == _UITableViewHeaderFooterContentView:0x101e18990.right - 10.0>",
"<NSLayoutConstraint:0x600002127520 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x101e18990.width == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101e180e0.right == _UITableViewHeaderFooterContentView:0x101e18990.right - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#75 UIButton:0x101e180e0.top == _UITableViewHeaderFooterContentView:0x101e18990.top + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101e180e0.bottom == _UITableViewHeaderFooterContentView:0x101e18990.bottom - 10.0>",
"<NSLayoutConstraint:0x600002127570 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x101e18990.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101e180e0.bottom == _UITableViewHeaderFooterContentView:0x101e18990.bottom - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#73 UIButton:0x101b2f160.left == _UITableViewHeaderFooterContentView:0x101b2fb70.left + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101b2f160.right == _UITableViewHeaderFooterContentView:0x101b2fb70.right - 10.0>",
"<NSLayoutConstraint:0x600002132710 'UIView-Encapsulated-Layout-Width' _UITableViewHeaderFooterContentView:0x101b2fb70.width == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#74 UIButton:0x101b2f160.right == _UITableViewHeaderFooterContentView:0x101b2fb70.right - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:[email protected]#75 UIButton:0x101b2f160.top == _UITableViewHeaderFooterContentView:0x101b2fb70.top + 10.0>",
"<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101b2f160.bottom == _UITableViewHeaderFooterContentView:0x101b2fb70.bottom - 10.0>",
"<NSLayoutConstraint:0x600002132760 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x101b2fb70.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:[email protected]#76 UIButton:0x101b2f160.bottom == _UITableViewHeaderFooterContentView:0x101b2fb70.bottom - 10.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Similar issue was asked here before:
Constraints on a UITableViewHeaderFooterView result in UIViewAlertForUnsatisfiableConstraints
However, I already have implemented the solution from there about setting the sectionHeaderHeight and estimatedSectionHeaderHeight to UITableView.automaticDimension and I still keep getting the warnings.
I have also tried setting the priority of the bottom and right constraints of sectionTitle to .low but then the text just goes off screen.
The actual layout looks good, so I think I am missing something simple. How do I prevent those warnings?

.bottomconstraint makes the warning about the bottom go away. However, setting the priority of the.rightconstraint takes away the warning about right but also causes the text to no longer wrap and goes off screen instead.