根据屏幕大小更改 UICollectionView 单元格大小
change UICollectionView Cell size depending on the screen size
我能够使用下面的函数更改 collectionView 单元格大小,但我如何才能使此函数适用于不同的屏幕尺寸。例如,我希望 iPad 的高度和宽度不同 iPhone 有没有办法做到这一点
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
return CGSize(width: (width/5.5), height: (height/4.5))
}
它很容易检测屏幕尺寸,对于 iPhone/iPad 的每个屏幕尺寸。跟着这个东西就行了。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
if UIDevice().userInterfaceIdiom == .phone
{
switch UIScreen.main.nativeBounds.height
{
case 480:
print("iPhone Classic")
case 960:
print("iPhone 4 or 4S")
case 1136:
print("iPhone 5 or 5S or 5C")
case 1334:
print("iPhone 6 or 6S")
case 2208:
print("iPhone 6+ or 6S+")
default:
print("unknown")
}
}
if UIDevice().userInterfaceIdiom == .pad
{
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366))
{
print("iPad Pro : 12.9 inch")
}
else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1024 || UIScreen.main.bounds.size.width == 1024))
{
print("iPad 2")
print("iPad Pro : 9.7 inch")
print("iPad Air/iPad Air 2")
print("iPad Retina")
}
else
{
print("iPad 3")
}
}
}
所以使用它来根据您的要求设置高度和宽度。
即使您可以 perform/set 特定屏幕的布局。
希望对您有所帮助。
我能够使用下面的函数更改 collectionView 单元格大小,但我如何才能使此函数适用于不同的屏幕尺寸。例如,我希望 iPad 的高度和宽度不同 iPhone 有没有办法做到这一点
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
return CGSize(width: (width/5.5), height: (height/4.5))
}
它很容易检测屏幕尺寸,对于 iPhone/iPad 的每个屏幕尺寸。跟着这个东西就行了。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
if UIDevice().userInterfaceIdiom == .phone
{
switch UIScreen.main.nativeBounds.height
{
case 480:
print("iPhone Classic")
case 960:
print("iPhone 4 or 4S")
case 1136:
print("iPhone 5 or 5S or 5C")
case 1334:
print("iPhone 6 or 6S")
case 2208:
print("iPhone 6+ or 6S+")
default:
print("unknown")
}
}
if UIDevice().userInterfaceIdiom == .pad
{
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366))
{
print("iPad Pro : 12.9 inch")
}
else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1024 || UIScreen.main.bounds.size.width == 1024))
{
print("iPad 2")
print("iPad Pro : 9.7 inch")
print("iPad Air/iPad Air 2")
print("iPad Retina")
}
else
{
print("iPad 3")
}
}
}
所以使用它来根据您的要求设置高度和宽度。 即使您可以 perform/set 特定屏幕的布局。
希望对您有所帮助。