为什么UITableViewCell报表标签框架的layoutSubviews设为0?

时间:2019-01-23 06:39:58

标签: ios uitableview constraints

我创建了一个非常简单的contentView,仅带有标签,然后将标签限制在单元格的layoutSubviews()范围内。

(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) } } ,但是运行时我可以清楚地看到标签的框架不是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() } } 中实例化单元格的方式。

$(window).scroll(function() {
    if (window.innerWidth <= 768) {
        let scrollStatus = $(this).scrollTop();

        if (scrollStatus > lastScrollTop) {
            //do some stuffs

        else
            //do some other stuffs

        lastScrollTop = scrollStatus;
    }
});

0 个答案:

没有答案