我是一名android开发人员,正在开发我的第一个iOS
应用程序,我需要实现GridView才能添加一些社交图标,因此在搜索后发现UICollectionView
,但未按预期工作。如何设置UICollection视图的动态高度?其实我想显示所有图标,但它显示滚动UICollectionView后只显示一行和其他人。UIViewController中的动态UICollectionView
是例如: 这是我得到
这就是我想要的:
我使用下面的代码:
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])
}
}
任何帮助,将不胜感激。
你可以使用autoLayout吗? –
使用自动布局:此集合视图的出口高度,高度:NSLayoutConstraint! - >在此之后Height.constant = 52(根据您的要求) –