Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customView 未添加到 window,调用 layoutIfNeeded 会有警告 #31

Open
tisfeng opened this issue Mar 15, 2021 · 4 comments
Open

Comments

@tisfeng
Copy link

tisfeng commented Mar 15, 2021

作者你好,感谢你的代码开源!

这是我的代码使用,点击按钮,从底部弹出一个列表:

    @objc func buttonClick(_ button: UIButton) {
        let tableView = MyTableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200))
        
        let popView = LSTPopView.initWithCustomView(tableView, parentView: nil, popStyle: .smoothFromBottom, dismissStyle: .smoothToBottom)
        
        popView?.hemStyle = .bottom
        popView?.isClickBgDismiss = true
        popView?.pop()
    }

日志有如下警告:

[TableView] 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. Table view: <UITableView: 0x7fd841840a00; frame = (0 0; 390 200); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600002082e20>; layer = <CALayer: 0x600002edce20>; contentOffset: {0, 0}; contentSize: {390, 440}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <OneWord.MyTableView: 0x7fd840413430; frame = (0 0; 390 200); gestureRecognizers = <NSArray: 0x600002066e50>; layer = <CALayer: 0x600002ed6920>>>

详情如图:

image

@tisfeng
Copy link
Author

tisfeng commented Mar 15, 2021

测试确认是 [self.customView layoutIfNeeded] 这行代码导致。

找到个类似的问题 https://github.com/wordpress-mobile/WordPress-iOS/issues/12689, 这里提供的一种解决思路是判断 view 的 window 是否存在: wordpress-mobile/WordPress-iOS#12689 (comment)

  guard view.window != nil else {
            return
        }

@tisfeng
Copy link
Author

tisfeng commented Mar 15, 2021

另一个类似问题: https://juejin.cn/post/6844904111536603143

@LoSenTrad
Copy link
Owner

@tisfeng 加群或者加我微信 详聊问题 目前没遇到这种情况 可能跟你自定义view布局错误有关

@tisfeng
Copy link
Author

tisfeng commented Mar 17, 2021

好的,我先自己检查一下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants