Я создал очень простой UITableViewCell1
без метки и ограничил метку contentView
границами ячейки.
В layoutSubviews()
, если я печатаю рамку этикетки, она сообщает рамку ярлыка как (0.0, 0.0, 0.0, 0.0)
, но при запуске я вижу, что рамка этикетки не равна 0.
Я что-то в корне неправильно понимаю?
class SimpleTitleLabelCell: UITableViewCell {
lazy private(set) var titleLabel: UILabel = {
let titleLabel = UILabel()
self.contentView.addSubview(titleLabel)
titleLabel.numberOfLines = 0
titleLabel.text = "Title"
titleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
titleLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor),
titleLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
titleLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
])
return titleLabel
}()
override func layoutSubviews() {
super.layoutSubviews()
print("\(self.titleLabel.frame)")
//prints (0.0, 0.0, 0.0, 0.0)
}
}
Вот как я создаю экземпляр ячейки в TableViewController
.
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 100
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return SimpleTitleLabelCell()
}
}