ask*_*ale 2 layout ipad ios uicollectionview swift
目前在iPad上使用集合视图遇到Flow布局的一些问题.我目前正在开发一个应用程序,它以流布局显示帖子,使其看起来美观而时尚.但是,在所有设备上测试应用程序时,我注意到显示更改:
但剩下的iPad,它显示正常.这里似乎有什么问题?我目前正在使用此代码在CollectionView中设置我的布局样式:
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
}
}
Run Code Online (Sandbox Code Playgroud)
'iPad Two'和'iPad Three'只是我试图创建的一些枚举,试图确定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
}
Run Code Online (Sandbox Code Playgroud)
这里是我的问题的直观表示,在列出的设备上发生,但不在其余部分:
它看起来应该是这样的:
列出的设备上看起来像什么:
我真的很感激帮助,如果有人可以帮我解决这个问题,因为我似乎无法找到一个正常工作的解决方案.
仅供参考.我建议,不要设置CollectionCell Size sizeForItemAt indexPath,尝试一些通用的方法.
你应该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
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,将您的CollectionView的 UICollectionViewFlowLayout,以HomeLayout从界面生成器.
调整sectionInsets和Min Spacing从Storyboard.
仅供参考.相应地设置你numberOfItemsPerRow的HomeLayout.
| 归档时间: |
|
| 查看次数: |
540 次 |
| 最近记录: |