iosswiftuicollectionviewiphone-xuiedgeinsets

Why does iPhone X ignore UIEdgeInsets for UICollectionView?


I have code for my collectionView that adjusts the content so that it sits beneath the navigation bar.

        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)

This works well on every other device on iOS 11 except for iPhone X, on iPhone X, the content sits behind the nav bar and toolbar on app start.

Is there something I am missing for iPhone X specifically?

Thanks


Solution

  • I think you forgot to calculate the height of status bar. Before iPhone X, the height of status bar is 20pt and in iPhoneX it is 44pt. That's the reason you can not see the complete cell.

    To do that, add your constraints from superview and write the following code:

        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)
    

    Hope this helps :)