So I'm adding views into a tableviewcell's content view programmatically and using the dynamic height that tableview's offer and I seem to be getting the following autolayout warnings, should I be worried since the layout doesn't seem to be messed up but it's annoying to have a console full of these warnings.
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.
2018-10-09 14:51:46.748606+0100 GenericForms[78197:5088471] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55 (active)>",
"<NSLayoutConstraint:0x600001018960 V:|-(20)-[UIView:0x7fa582d162e0] (active, names: '|':UITableViewCellContentView:0x7fa582d15800 )>",
"<NSLayoutConstraint:0x60000101c910 UIView:0x7fa582d162e0.bottom == UITableViewCellContentView:0x7fa582d15800.bottom - 20 (active)>",
"<NSLayoutConstraint:0x60000103d950 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa582d15800.height == 95 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55 (active)>
This is the code I'm using to add a view within the UITableViewCell
content view content.
if view == nil {
view = UIView(frame: .zero)
view?.backgroundColor = .green
view?.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(view!)
NSLayoutConstraint.activate([
view!.heightAnchor.constraint(equalToConstant: 55),
view!.topAnchor.constraint(equalTo: contentView.topAnchor, constant: edgeInsets.top),
view!.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -edgeInsets.bottom),
view!.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: edgeInsets.left),
view!.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -edgeInsets.right)
])
}
Within my view controller this the code that I'm using to set the table view.
class SelfSizedTableView: UITableView {
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
self.layoutIfNeeded()
}
override var intrinsicContentSize: CGSize {
let height = min(contentSize.height, maxHeight)
return CGSize(width: contentSize.width, height: height)
}
}
class FormViewController: UIViewController {
private let tableView: SelfSizedTableView = {
let tv = SelfSizedTableView()
tv.isScrollEnabled = false
tv.translatesAutoresizingMaskIntoConstraints = false
tv.tableFooterView = UIView()
tv.register(TestTableViewCell.self, forCellReuseIdentifier: "cellId")
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
tableView.dataSource = self
view.addSubview(tableView)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
centerTableViewContent()
}
private func centerTableViewContent() {
tableView.removeConstraints(tableView.constraints)
if tableView.intrinsicContentSize.height > view.safeAreaLayoutGuide.layoutFrame.size.height {
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
tableView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor),
tableView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor)
])
} else {
NSLayoutConstraint.activate([
tableView.heightAnchor.constraint(equalToConstant: tableView.intrinsicContentSize.height),
tableView.widthAnchor.constraint(equalToConstant: view.frame.width),
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
}
}
extension FormViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! TestTableViewCell
switch indexPath.row {
case 0:
cell.backgroundColor = .red
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
case 1:
cell.backgroundColor = .yellow
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10))
default:
cell.backgroundColor = .blue
cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 70, left: 10, bottom: 70, right: 10))
}
return cell
}
}