Swift: safe area on iPhone

我只是一个虾纸丫 提交于 2020-01-25 09:01:08

问题


I have ViewController with collectionView and collectionView bottom, top, leading, trailing constraints to superview. And on iPhone 8 my screen look like this:

And for another devices all looks fine. But on iPhone X look like this:

I use this code for my cells:

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) 

    }

How to fix it?

Update

constraints:


回答1:


With your details I got below output.

NOTE: In landscape mode safearea of top is zero.

Constraints :

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

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



来源:https://stackoverflow.com/questions/51184906/swift-safe-area-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!