记述UITableView超前Layout的一个问题

626 阅读1分钟

Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.

机器翻译了一下:

警告仅一次:UITableView被告知布局其可见单元格和其他内容,而不在视图层次结构中(表视图或其超视图之一尚未添加到窗口)。这可能会导致错误,迫使表视图内的视图在没有准确信息的情况下加载和执行布局(例如,表视图边界、特征集合、布局边距、安全区域插入等),还将由于额外的布局传递而导致不必要的性能开销。在UitableViewAlertForLayoutSideViewHierarchy上创建一个符号断点,以在调试器中捕捉到这一点,并查看导致这一情况的原因,因此,如果可能的话,您可以完全避免此操作,或者将其延迟到将表视图添加到窗口中。

仔细检查代码发现在viewdidload方法里我写了self.view.layoutIfNeeded()强制去提前Layout视图导致的。