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)") }
// sizeforItemAtIndexPath
func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return imageSize! }
iPhone 6结果:
结果在iPhone 5s
Objective-C / Swift对于解决方案都很好.
谢谢.
步骤2:使用UICollectionVIEwDelegateFlowLayout的委托方法.
func collectionVIEw(collectionVIEw: UICollectionVIEw,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize{return CGSizeMake((UIScreen.mainScreen().bounds.wIDth-15)/4,120); //use height whatever you wants.}
步骤3:转到XIB或StoryBoard,您拥有您的CollectionVIEw.
步骤4:在XIB或StoryBoard中,您有CollectionVIEw单击CollectionVIEw.
步骤5:转到InterfaceBuilder,然后在最后一个选项卡(即:尺寸检查器)中设置最小间距
对于细胞= 5
对于线= 5
就这样
Note:
This solution is for if you wants to make spacing between cell = 5. If your spacing is different then 5,then you need to change value 15 in delegate method. Ex: Spacing between cell = 10,then change delegate 15 to 10×3 = 30
return CGSizeMake((UIScreen.mainScreen().bounds.wIDth-30)/4,120);
>并设置最小间距
For Cells = 10
For lines = 10
反之亦然.
Swift 3版本
func collectionVIEw(_ collectionVIEw: UICollectionVIEw,sizeforItemAt indexPath: IndexPath) -> CGSize { let wIDth = UIScreen.main.bounds.wIDth return CGSize(wIDth: (wIDth - 10)/3,height: (wIDth - 10)/3) // wIDth & height are the same to make a square cell}总结
以上是内存溢出为你收集整理的ios – 基于设备尺寸的UICollectionView单元格间距全部内容,希望文章能够帮你解决ios – 基于设备尺寸的UICollectionView单元格间距所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)