Swift:iPhone上的安全区域

时间:2018-07-05 06:47:48

标签: ios swift uiviewcontroller iphone-x

我有ViewController,其中collectionViewcollectionView的底部,顶部,前导,尾随约束用于超级视图。在iPhone 8上,我的屏幕看起来像这样:

enter image description here

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

enter image description here

我在我的单元格中使用以下代码:

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) 

    }

如何解决?

更新

约束:

enter image description here

1 个答案:

答案 0 :(得分:0)

关于您的详细信息,我得到的输出如下。

enter image description here

  

注意:在横向模式下,顶部的安全区域为零。

约束:

enter image description here

SizeForItem:

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)
    }
  

编辑:检查此o / p是否不适合您。所以我会解释。而且,如果您想覆盖安全区域,那是不可能的。

iPhone X

enter image description here

iPhone 8

enter image description here

相关问题