Iphone 如何使用Swift 3检查Xcode中特定于iOS的设备?

Iphone 如何使用Swift 3检查Xcode中特定于iOS的设备?,iphone,swift3,xcode8,ios10,Iphone,Swift3,Xcode8,Ios10,我正在设置集合视图,但在为每个应用程序大小定义单元格的正确大小时遇到问题,因此我决定通过flowLayout.itemSize.width和flowLayout.itemSize.height手动执行此操作,但要以这种方式执行,我需要针对每个设备进行检查,与iPhone 6/7和iPhone 6/7 Plus一样,可以正确设置手机的大小。那么,如何针对特定设备进行检查以解决问题?不要针对设备类型进行检查。看看屏幕大小。您可以通过UIScreen类轻松做到这一点。不要检查设备类型。看看屏幕大小。您

我正在设置集合视图,但在为每个应用程序大小定义单元格的正确大小时遇到问题,因此我决定通过flowLayout.itemSize.width和flowLayout.itemSize.height手动执行此操作,但要以这种方式执行,我需要针对每个设备进行检查,与iPhone 6/7和iPhone 6/7 Plus一样,可以正确设置手机的大小。那么,如何针对特定设备进行检查以解决问题?

不要针对设备类型进行检查。看看屏幕大小。您可以通过UIScreen类轻松做到这一点。

不要检查设备类型。看看屏幕大小。您可以通过UIScreen类轻松实现这一点。

因此,这就是我解决问题的基本方法。我首先得到了每个设备的宽度和高度的正确大小,然后根据它进行了检查。在每个条件中,我完成了collectionView单元格的设计

func adjustPetCellDesign(){ //获取flowLayout以设置petCell的边距 guard let flowLayout=collectionView.collectionViewLayout as?UICollectionViewFlowLayout else{return} flowLayout.minimumInteritemSpacing=边距 flowLayout.minimumLineSpacing=边距 flowLayout.sectionInset=UIEdgeInSet(顶部:页边距,左侧:页边距,底部:页边距,右侧:页边距)


所以,这就是我解决问题的方法。我首先得到了每个设备的宽度和高度的正确大小,然后根据它进行检查。在每个条件中,我完成了collectionView单元格的设计

func adjustPetCellDesign(){ //获取flowLayout以设置petCell的边距 guard let flowLayout=collectionView.collectionViewLayout as?UICollectionViewFlowLayout else{return} flowLayout.minimumInteritemSpacing=边距 flowLayout.minimumLineSpacing=边距 flowLayout.sectionInset=UIEdgeInSet(顶部:页边距,左侧:页边距,底部:页边距,右侧:页边距)


您不需要获取设备,只需使用screenbood.size并使用它设置项目高度/witdh即可。您不需要获取设备,只需使用screenbood.size并使用它设置项目高度/witdh即可
    //Get the screenSize to get width and height of every device.
    let screenSize: CGRect = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    //If iPhone is 6(s) or 7(s), set width and height as shown below
    if screenWidth == 375 && screenHeight == 667 {
        flowLayout.itemSize.height = 171.5
        flowLayout.itemSize.width = 171.5

        //If iPhone is 6(s)/7(s) Plus, set width and height as shown below
    } else if screenWidth == 414 && screenHeight == 736 {
        flowLayout.itemSize.height = 194
        flowLayout.itemSize.width = 194

        //If iPhone is 5(s)/SE, set width and height as shown below
    } else if screenWidth == 320 && screenHeight == 568 {
        flowLayout.itemSize.height = 147.5
        flowLayout.itemSize.width = 147.5
    }
}