gpt4 book ai didi

swift - UICollectionViewCell 的自定义高度被忽略

转载 作者:行者123 更新时间:2023-12-01 21:50:26 26 4
gpt4 key购买 nike

我已经设置了一个 UICollectionView,如下所示。我正在尝试设置自定义单元格高度,以便该集合在必须滚动之前适合屏幕上的 3 个单元格。但是,这种定制工作不起作用或似乎被忽略了。有谁知道我做错了什么?

class CoopOverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var CampaignBrandSliderBackground: UIView!
@IBOutlet weak var CampaignBrandSlider: UISegmentedControl!
@IBOutlet weak var CampaignCollectionView: UICollectionView!

// TODO: load real data and replace with campaign model
let campaignImages: [UIImage] = [UIImage(named: "icon-black")!,UIImage(named: "icon-white")!,UIImage(named: "icon-black")!,UIImage(named: "icon-white")!]
let apiService = APIService()

override func viewDidLoad() {
super.viewDidLoad()
CampaignBrandSliderBackground.layer.cornerRadius = 10

// do other stuff...

// Conform to UICollectionViewDelegate Protocol:
CampaignCollectionView.dataSource = self
CampaignCollectionView.delegate = self


// Adjust Layout of CollectionViewCell: set cell height so that the collection fits 3 cells
var cellLayout = self.CampaignCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
cellLayout.itemSize = CGSize(width: self.CampaignCollectionView.frame.size.width, height: self.CampaignCollectionView.frame.size.height/3)

//???: 自定义高度似乎被忽略了

    // Conform to UICollectionViewDataSource Protocol:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return campaignImages.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let campaignCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CampaignTileViewCell", for: indexPath) as! CampaignTileViewCellController
campaignCell.CampaignBackgroundImage.image = campaignImages[indexPath.item]

return campaignCell
}

最佳答案

UICollectionView 有一个Estimate Size 属性,它限制了要设置的自定义高度。您可以从 Storyboard 中更新其值(value)。选择 UICollectionView -> Size Inspector -> Estimate Size 并将其值设置为 None。这样您的自定义高度就会像魅力一样发挥作用!

关于swift - UICollectionViewCell 的自定义高度被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com