Ios Swift UICollectionView iPad流布局不工作
当前在iPad上使用集合视图时遇到一些流布局问题。我目前正在开发一个应用程序,以流式布局显示帖子,使其看起来既美观又时尚。但是,当在所有设备上测试应用程序时,我注意到显示器在以下位置发生变化:Ios Swift UICollectionView iPad流布局不工作,ios,swift,ipad,layout,uicollectionview,Ios,Swift,Ipad,Layout,Uicollectionview,当前在iPad上使用集合视图时遇到一些流布局问题。我目前正在开发一个应用程序,以流式布局显示帖子,使其看起来既美观又时尚。但是,当在所有设备上测试应用程序时,我注意到显示器在以下位置发生变化: 第五代iPad iPad Air iPad Air 2 iPad Pro(9.7英寸) 但对于其余的iPad,它可以正常显示。这里有什么问题吗?我当前正在使用此代码在CollectionView中设置布局样式: func collectionView(_ collectionView: UIColl
- 第五代iPad
- iPad Air
- iPad Air 2
- iPad Pro(9.7英寸)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch UIDevice().screenType {
case .iPhoneX:
return CGSize(width: 180.0, height: 180.0)
break
case .iPhone5:
return CGSize(width: 152.0, height: 152.0)
break
case .iPhone6:
return CGSize(width: 180.0, height: 180.0)
break
case .iPhone6Plus:
return CGSize(width: 200.0, height: 200.0)
break
case .iPad:
return CGSize(width: 400.0, height: 400.0)
break
case .iPadTwo:
return CGSize(width: 400.0, height: 400.0)
break
case .iPadThree:
return CGSize(width: 400.0, height: 400.0)
break
default:
return CGSize(width: 200.0, height: 200.0)
break
}
}
“ipadtwo”和“ipadtwo”只是我试图创建的一些枚举,试图确定iPad的类型,以及我是否得到打印一些文本的输出(但它只是下降到默认值),而不考虑iPad
这是我的屏幕尺寸:
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhone4
case 1136:
return .iPhone5
case 1334:
return .iPhone6
case 2208:
return .iPhone6Plus
case 1024:
return .iPad
case 1366:
return .iPadTwo
case 834:
return .iPadThree
case 2436:
return .iPhoneX
break
default:
return .unknown
}
下面是我的问题的一个可视化表示,出现在列出的设备上,而不是其他设备上:
它应该是什么样子的:
它在列出的设备上的外观:
如果有人能帮我解决这个问题,我将不胜感激,因为我似乎找不到一个正常工作的解决方案。FYI。我建议,不要在
sizeForItemAt indexPath
中设置CollectionCell大小,尝试一些通用方法
您应该扩展UICollectionViewFlowLayout
并创建如下新布局
class HomeLayout: UICollectionViewFlowLayout {
var numberOfItemsPerRow : Int {
get{
var value = 0
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
value = 3
} else {
value = 5
}
return value
}
set {
self.invalidateLayout()
}
}
override func prepare() {
super.prepare()
if self.collectionView != nil {
var newItemSize = self.itemSize
let itemsPerRow = max(self.numberOfItemsPerRow, 1)
let totalSpacing = self.minimumInteritemSpacing * CGFloat(itemsPerRow - 1)
let newWidth = (self.collectionView!.bounds.size.width - self.sectionInset.left - self.sectionInset.right - totalSpacing) / CGFloat(itemsPerRow)
newItemSize.width = max(newItemSize.width,newWidth)
if self.itemSize.height > 0 {
let aspectRatio: CGFloat = self.itemSize.width / self.itemSize.height
newItemSize.height = newItemSize.width / aspectRatio
}
self.itemSize = newItemSize
}
}
}
现在从Interface builder将您的CollectionView的UICollectionViewFlowLayout
分配给HomeLayout
从情节提要
调整部分插图
和最小间距
仅供参考。在
主页布局中相应地设置numberOfItemsPerRow
。我会试试。我试过之后会给你反馈。谢谢。我还是不能让它正常工作。可能是因为我没有设置项目编号箭头?我该如何正确设置呢?假设iPhone需要每行3个项目,然后设置值=3,而iPad则不需要显式设置,iPhone默认为3,iPad默认为5,如果你想显式设置,你需要做一些代码工作,比如魅力,太棒了!非常感谢你!请参考此链接:并尝试这样做。