Swift:iPhone 上的安全区域 [英] Swift: safe area on iPhone

查看:72
本文介绍了Swift:iPhone 上的安全区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) ->CG尺寸{让偏移量:CGFloat = 10让宽度 = collectionView.bounds.width/3 - 偏移量 * 4让 a: CGFloat = 宽度/2让 b: CGFloat = a * 3 + a * 0.65让高度 = b返回CGSize(宽:宽,高:高)}

如何解决?

更新

约束:

解决方案

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

<块引用>

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

约束:

SizeForItem :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) ->CG尺寸{让偏移量:CGFloat = 10让宽度 = collectionView.bounds.width/3 - 偏移量 * 4让 a: CGFloat = 宽度/2让 b: CGFloat = a * 3 + a * 0.65让高度 = b返回CGSize(宽:宽,高:高)}

<块引用>

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

iPhone X

iPhone 8

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:

解决方案

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

这篇关于Swift:iPhone 上的安全区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆