如何更改2x2 UICollectionView的大小并适合整个屏幕:Swift 3 Xcode 8 [英] How do I change the size of a 2x2 UICollectionView and fit the whole screen: Swift 3 Xcode 8
问题描述
我正在创建一个需要图像网格的应用程序.作为示例,我在UICollectionView的单元格中使用2x2的图像网格.我想知道如何减少中间的烦人空间,并使它们具有相等的宽度和高度以适合整个屏幕.我知道我可以使用Interface builder来做到这一点.但是,我想以编程方式执行此操作,因为随着应用程序的进展,将会有不同的级别和不同的正方形数-可能也是10x10.我是UICollectionViews的新手.我提到了许多其他问题,但是大多数问题都在Objective C中,而Swift中很少有似乎与Swift 3无关.
Hi I am creating an app which requires a grid of images. As an example I am using a 2x2 grid of images in cells of a UICollectionView. I would like to know how I could decrease the annoying space in the middle and also make them have equal widths and heights to fit the whole screen. I know that I can do this with Interface builder. However, I want to do this programmatically because as the app progresses, there will be different levels and different number of squares - possibly a 10x10 also. I am new to UICollectionViews. I have referenced to many other questions, however most are in Objective C and there few that are in Swift don't seem to work with 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
}
}
我还上传了有关当前状态和希望状态的图像-
I am also uploading an image of how it currently is and how I want it to be-
希望大家能帮助我-这将非常有用...谢谢!
I hope you guys will help me- it will be very useful... Thanks!
我遵循UICollectionViewDelegateFlowLayout并称为sizeforitematindexpath,但是它不起作用. 添加断点后,我意识到该方法没有被调用.所以我在这里做错什么了. Xcode也给我这个警告-
I conformed to UICollectionViewDelegateFlowLayout and called sizeforitematindexpath but it did not work. On adding a breakpoint I realized that the method was not getting called. So is there anything wrong I'm doing here. Also Xcode gives me this warning -
推荐答案
请确保:
- 确认
UICollectionViewDelegateFlowLayout
. - collectionView的宽度=屏幕的宽度.
- 单元格的最小间距为零.您可以从界面生成器"中进行更改(选择collectionView->显示尺寸检查器->将最小间距设置为0),也可以通过实现
minimumInteritemSpacingForSectionAt
返回零来更改它. - 如果要填充整个屏幕的高度(我问这是因为您的屏幕截图中没有提供此高度),则collectionView的高度=屏幕的高度.
- Confirming to
UICollectionViewDelegateFlowLayout
. - collectionView's width = screen's width.
- Min Spacing for cells is zero. You can change it from the Interface Builder (Select the collectionView -> show size inspector -> set min spacing to 0), or by implementing
minimumInteritemSpacingForSectionAt
returning zero. - If you want to fill the whole Screen height (I am asking because this is not what provided in your screenshot), collectionView's height = screen's height.
最后,实现sizeForItemAtIndexPath
方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width / 2, height: 100)
}
这篇关于如何更改2x2 UICollectionView的大小并适合整个屏幕:Swift 3 Xcode 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!