我已经实现了一个UICollectionView,单元格大小为w90 h90.当我在iPhone 6上运行时,我在单元格之间获得了适当的间距,但是当我在iPhone 5s上运行时,我在单元格之间获得了更多的间距.我的问题是我如何根据设备屏幕大小更改单元格大小,假设单元格大小为w80 h80,我也可以在iPhone 5s上获得正确的结果.我现在正在做的是
override func viewWillAppear(animated: Bool) {
var scale = UIScreen.mainScreen().scale as CGFloat
println("Scale :\(scale)")
var cellSize = (self.collectionViewLayout as UICollectionViewFlowLayout).itemSize
println("Cell size :\(cellSize)")
imageSize = CGSizeMake(cellSize.width * scale , cellSize.height * scale)
println("Image size : \(imageSize)")
}
Run Code Online (Sandbox Code Playgroud)
// sizeForItemAtIndexPath
func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return imageSize!
}
Run Code Online (Sandbox Code Playgroud)
iPhone 6上的结果:
iPhone 5s的结果
Objective-C/Swift对解决方案都很好.谢谢.