Я имею tableView с footerView . На нем должна отображаться простая метка.
В моем ViewController в viewDidLoad
я назначаю tableFooterView следующим образом:
let footerView = MyFooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 0))
tableView.tableFooterView = footerView
MyFooterView
- это UIView с одной меткой. Настройка метки выглядит следующим образом:
label.font = someFont
label.adjustsFontForContentSizeCategory = true
label.textColor = .black
label.numberOfLines = 0
label.text = "my super looooooong label that should break some lines but it doesn't."
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 40),
label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -40),
label.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -20)
])
Чтобы заставить AutoLayout работать с MyFooterView
, я вызываю этот метод внутри UIViewControllers viewDidLayoutSubviews
:
func sizeFooterToFit() {
if let footerView = self.tableFooterView {
footerView.setNeedsLayout()
footerView.layoutIfNeeded()
let height = footerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
var frame = footerView.frame
frame.size.height = height
footerView.frame = frame
self.tableFooterView = footerView
}
}
Проблема: Строки в метке не ломаются. Я получаю следующий результат:
Что я могу сделать, чтобы на метке было несколько строк? AutoLayout работает благодаря метод sizeFooterToFit
. Единственное, что высота надписей - всего одна строчка.