Swift:iPhone 上的安全区域

Swift: safe area on iPhone

我有 ViewControllercollectionViewcollectionView 底部、顶部、前导、尾随约束到超级视图。在 iPhone 8 上,我的屏幕如下所示:

对于其他设备,一切看起来都很好。但是在 iPhone X 上看起来像这样:

我将此代码用于我的单元格:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let offset: CGFloat = 10
    let width = collectionView.bounds.width / 3  - offset * 4
    let a: CGFloat = width / 2
    let b: CGFloat = a * 3 + a * 0.65
    let height = b

    return CGSize(width: width, height: height) 

    }

如何解决?

更新

约束条件:

根据你的详细信息,我得到了以下输出。

NOTE: In landscape mode safearea of top is zero.

约束:

项目大小:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let offset: CGFloat = 10
        let width = collectionView.bounds.width / 3  - offset * 4
        let a: CGFloat = width / 2
        let b: CGFloat = a * 3 + a * 0.65
        let height = b

        return CGSize(width: width, height: height)
    }

Edit : Check is this o/p is not for you or not. So that I will explain. And if you wanted to cover the safe area then it is not possible.

Iphone X

Iphone 8