为什么 iPhone X 会忽略 UICollectionView 的 UIEdgeInsets?

Why does iPhone X ignore UIEdgeInsets for UICollectionView?

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

        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 有什么我特别想念的吗?

谢谢

我想你忘了计算状态栏的高度。 在iPhoneX之前,状态栏的高度是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)

希望对您有所帮助:)