嗨,我正在创建一个需要图像网格的应用程序。作为一个例子,我在UICollectionView的单元格中使用2x2网格图像。我想知道如何减少中间的恼人空间,并使它们具有相同的宽度和高度,以适应整个屏幕。我知道我可以使用Interface builder来完成这项工作。但是,我想以编程方式执行此操作,因为随着应用程序的进展,将有不同级别和不同数量的正方形 - 也可能是10x10。我是UICollectionViews的新手。我引用了许多其他问题,但是大多数问题都在Objective C中,并且Swift中的一些问题似乎与Swift 3无关。
这是我的完整代码:
import UIKit
class GameViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
collectionView.delegate = self
collectionView.dataSource = self
let collectionViewLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.minimumInteritemSpacing = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2;
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let image = cell.viewWithTag(1) as! UIImageView
image.image = #imageLiteral(resourceName: "coffee")
return cell;
}
func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.size.width/2, height: self.collectionView.frame.size.height/2)
//this method is not getting called
}
}
我还上传了一份关于它目前是如何以及我希望它如何的图像 -
我希望你们能帮助我 - 这将非常有用......谢谢!
我符合UICollectionViewDelegateFlowLayout并调用sizeforitematindexpath,但它不起作用。在添加断点时,我意识到该方法没有被调用。所以我在这里做错了。 Xcode也给了我这个警告 -
确保:
UICollectionViewDelegateFlowLayout
。minimumInteritemSpacingForSectionAt
更改它。最后,实现sizeForItemAtIndexPath
方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width / 2, height: 100)
}
在viewDidLoad中使用以下代码
let collectionViewLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.minimumInteritemSpacing = 0
collectionViewLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.width/2)
使用“UICollectionViewDelegateFlowLayout”委托CollectionView单元格大小的集合大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.width/2)
}
我希望这对你有所帮助。
您可以使用以下内容来调整单元格的大小:
extension GameViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth = self.collectionView.bounds.width / 2
return CGSize(width: cellWidth, height: cellWidth)
}
}
另外,在viewDidLoad()
的末尾添加以下行以消除不需要的空白区域:
let collectionViewLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.minimumInteritemSpacing = 0