如何更改 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

查看:26
本文介绍了如何更改 2x2 UICollectionView 的大小并适合整个屏幕: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 的宽度 = 屏幕的宽度.
  • 单元格的最小间距为零.您可以从 Interface Builder(选择 collectionView -> 显示大小检查器 -> 将最小间距设置为 0)或通过实现 minimumInteritemSpacingForSectionAt 返回零来更改它.
  • 如果你想填满整个屏幕高度(我问是因为这不是你的截图中提供的),collectionView 的高度 = 屏幕的高度.

最后实现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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆