我是一名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(根據您的要求) –