Der mehrzeilige Titel in UIButton in einer UITableViewHeaderFooterView führt zu UIViewAlertForUnsatisfiableConstraints-WIOS

Programmierung für iOS
Anonymous
 Der mehrzeilige Titel in UIButton in einer UITableViewHeaderFooterView führt zu UIViewAlertForUnsatisfiableConstraints-W

Post by Anonymous »

Ich muss gelegentlich lange Texte in einem UIButton in der Abschnittsüberschrift meiner UITableView anzeigen. Der folgende einfache Beispielcode zeigt das Problem:

Code: Select all

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)
}
}

}

}
Das tatsächliche Layout ist in Ordnung und der Text wird korrekt angezeigt, wenn längerer Text umbrochen wird:
Image

Xcode druckt jedoch weiterhin die folgenden UIViewAlertForUnsatisfiableConstraints Einschränkungen können nicht gleichzeitig erfüllt werden Warnungen:

Code: Select all

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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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.
(
"",
"",
""
)

Will attempt to recover by breaking constraint


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  may also be helpful.
Ein ähnliches Problem wurde hier schon einmal gestellt:
Einschränkungen für eine UITableViewHeaderFooterView führen zu UIViewAlertForUnsatisfiableConstraints
Allerdings habe ich die Lösung von dort zum Festlegen von sectionHeaderHeight und geschätzterSectionHeaderHeight auf UITableView.automaticDimension bereits implementiert und ich bekomme immer noch die Warnungen.
Ich habe auch versucht, die Priorität der unteren und rechten Einschränkungen von sectionTitle auf .low zu setzen, aber dann verschwindet der Text einfach vom Bildschirm.
Das tatsächliche Layout sieht gut aus, also denke ich, dass mir etwas Einfaches fehlt. Wie verhindere ich diese Warnungen?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post