2016-12-29 21 views
0

我是一名android开发人员,正在开发我的第一个iOS应用程序,我需要实现GridView才能添加一些社交图标,因此在搜索后发现UICollectionView,但未按预期工作。如何设置UICollection视图的动态高度?其实我想显示所有图标,但它显示滚动UICollectionView后只显示一行和其他人。UIViewController中的动态UICollectionView

下面

是例如: 这是我得到

enter image description here

这就是我想要的:

enter image description here

我使用下面的代码:

import UIKit 

class CollectionViewDemo: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

@IBOutlet weak var socialHandleCollection: UICollectionView! 


@IBOutlet weak var socialImageView: UIImageView! 

var socialHandleArray:[String] = ["Facebook", "Twitter", "Youtube","Vimeo", "Instagram", "Custom URL", "Linkedin", "pinterest"] 


override func viewDidLoad() { 

    self.socialHandleCollection.delegate = self 
    self.socialHandleCollection.dataSource = self 

    // socialHandleCollection.frame.size.height = 130 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return socialHandleArray.count 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! colvwCell 

    cell.imgCell.image = UIImage(named: "demo_img.png") 

    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     print(self.socialHandleArray[indexPath.row]) 
} 
} 

任何帮助,将不胜感激。

+0

你可以使用autoLayout吗? –

+0

使用自动布局:此集合视图的出口高度,高度:NSLayoutConstraint! - >在此之后Height.constant = 52(根据您的要求) –

回答

0

假设你正在使用的autoLayout

首先声明,您希望先

let numberOfColumns = 5 

好了,所以第一件事情列的具体数量。你将不得不让你的课程符合UICollectionViewDelegateFlowLayout。现在实现的功能 -

func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize{ 

     return CGSize(width: collectionView.bounds.size.width/numberOfColumns, height: collectionView.bounds.size.width/numberOfColumns); 
} 

这里5可以改变你想在每一行和传递价值相同的高度将确保形状始终是方形的列数。因此,您可以应用圆角半径以使其成为圆形。

现在继续。从你的界面生成器,按Ctrl +单击和UICollectionView高度约束拖动到你的UIViewController(类似于你将如何为UIView做,但做到这一点的约束)

现在,一旦你知道你需要的项目数您UICollectionView里面显示,你可以这样做:

//replace 5 with the number of columns you want 
//array contains the items you wish to display 
func figureOutHeight(){ 

    if(array.count == 0){ 
     //as no items to display in collection view 
     return 
    } 

    //ceil function is just to round off the value to the next one, for example 6/5 will return 1 but we need 2 in this case. Ensure all arguments inside ceil function are float 
    let rowsCount = ceil(Float(array.count)/Float(numberOfColumns)) 

    //now you have number of rows soo just update your height constraint by multiplying row count with height of each item inside UICollectionView 
    collectionViewHeightConstraint.constant = rowsCount * collectionView.bounds.size.height/numberOfColumns; 

    view.layoutIfNeeded() 
} 

而且如果你没有改变滚动方向垂直。

+0

您添加的方法是针对Swift 3,而OP则使用Swift 2.x连击器正确处理第一个。 –

相关问题