为什么 iPhone X 忽略 UIEdgeInsets for UICollectionView



我的收藏视图有代码,可以调整内容,使其位于导航栏下方。

        collectionView.contentInsetAdjustmentBehavior = .never
        let tabBarHeight = self.tabBarController?.tabBar.bounds.height
        let navBarHeight = self.navigationController?.navigationBar.bounds.height
        self.edgesForExtendedLayout = UIRectEdge.all
        self.collectionView.contentInset = UIEdgeInsets(top: navBarHeight!, left: 0.0, bottom: tabBarHeight!, right: 0.0)

这在iOS 11上除iPhone X以外的所有其他设备上都运行良好,在iPhone X上,内容位于应用程序启动时的导航栏和工具栏后面。

iPhone X有什么特别缺少的东西吗?

谢谢

我想你忘了计算状态栏的高度。在iPhone X之前,状态栏的高度是20pt,在iPhoneX中是44pt。这就是您看不到完整单元格的原因。

为此,请从 superview 添加约束并编写以下代码:

    cv.contentInsetAdjustmentBehavior = .never
    let tabBarHeight = self.tabBarController?.tabBar.bounds.height ?? 0
    let statuBarHeight = UIApplication.shared.statusBarFrame.height
    let navBarHeight = self.navigationController?.navigationBar.bounds.height ?? 0
    self.edgesForExtendedLayout = UIRectEdge.all
    cv.contentInset = UIEdgeInsets(top: navBarHeight+statuBarHeight, left: 0.0, bottom: tabBarHeight, right: 0.0)

希望这对:)有所帮助

最新更新