2016-10-10 31 views
1

嗨,我正在创建一个需要网格图像的应用程序。作为一个例子,我在UICollectionView的单元格中使用了一个2x2的图像网格。我想知道如何减少中间的烦人空间,并让它们具有相同的宽度和高度以适应整个屏幕。我知道我可以使用Interface builder完成此操作。然而,我想以编程的方式做到这一点,因为随着应用程序的进展,会有不同的级别和不同数量的正方形 - 可能也是10x10。我是UICollectionViews的新手。我曾参考其他许多问题,但大多数都在目标C,似乎有一些是在斯威夫特不斯威夫特工作3如何更改2x2 UICollectionView的大小并适合整个屏幕:Swift 3 Xcode 8

这里是我的全码:

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 

} 




} 

我还上传的它目前是怎样一个形象,我怎么想它是─

enter image description here

enter image description here

我希望你们能帮助我 - 这将会非常有用......谢谢!

我符合UICollectionViewDelegateFlowLayout并称为sizeforitematindexpath但它没有工作。 在添加一个断点时,我意识到该方法没有被调用。那么,我在这里做什么错了。此外Xcode中给了我这样的警告 - enter image description here

+0

你可能要检查UIStackView –

回答

4

确保:

  • 确认到UICollectionViewDelegateFlowLayout
  • collectionView的宽度=屏幕的宽度。
  • 最小单元格间距为零。你可以从界面生成器中改变它(选择collectionView - >显示大小检查器 - >将最小间距设置为0),或者通过执行minimumInteritemSpacingForSectionAt返回零。
  • 如果你想填充整个屏幕高度(我问,因为这不是你的屏幕截图提供),collectionView的高度=屏幕的高度。

最后,实现sizeForItemAtIndexPath方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let screenSize: CGRect = UIScreen.main.bounds 

    // add the required height, if you want to fill the whole screen it should be 'screenSize.height/2' 
    return CGSize(width: screenSize.width/2, height: 100) 
} 
+0

感谢您的答复,但它似乎并没有工作..我注意到,如果我改变了界面生成器的单元格大小,单元大小也会在运行应用程序时发生变化,但是当我尝试通过符合UICollectionViewDelegateFlowLayout的代码编辑它时,它似乎保持不变。 –

+0

向返回线上的方法添加一个断点,它是否达到? –

+0

我认为我明白了你的观点:)你正在使用Swift 3,我编辑了代码片段。 –

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) 
    } 

我希望这将有帮助你。

+0

回报CGSize(宽度:UIScreen.main.bounds.width/2,高度:UIScreen.main.bounds.width/2)将使其平方基于UIScreen.main.bounds的宽度。 –

+1

是的,这将使方形。我们可以根据需要更改尺寸。 –

+0

谢谢你们的快速回复,我真的很感激它。然而,符合UICollectionViewDelegateFlowLayout似乎没有任何好处...我实现了sizeForItemAtIndexPath,但添加了一个断点,好像该方法没有被调用... –

1

您可以使用以下大小的细胞:

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 
+0

感谢您的答复,但它似乎不符合UICollectionViewDelegateFlowLayout做任何好... –

+0

哦,我只是在sizeForItemAt indexPath中放置一个断点,并意识到该方法没有被调用!我已经符合UICollectionViewDelegateFlowLayout ... –

+0

只是为了测试,尝试从collectionView IBOutlet中删除“弱”。 – Sebastian

3

使用下面的代码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) 
相关问题