[英]How to resize CollectionView cell height according to image in cell in swift?
[英]How to resize second cell in collectionview Swift?
我想在我的帖子之間添加原生廣告,但問題是第二個單元格是“adcell”,使用與“cell”相同的高度,我該如何更改“asdcell”的高度?
這是我的 adcell 代碼
collectionView.register(UINib(nibName: "AdViewCell", bundle: nil) , forCellWithReuseIdentifier: "adcell")
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
並展示廣告
let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell", for: indexPath) as! AdViewCell
if (indexPath.item % 5 == 1){
//admob code here
return adcell
此代碼用於設置帖子的高度
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
return CGSize(width: (((collectionView.frame.width) - 40) / 2), height: 500)
} else { //if iPhone
return CGSize(width: view.frame.width-20 , height: 500)
}
}
如果您知道每五個單元格就會添加一個單元格(順便說一句,如果您想實現這一點, indexPath.item % 5 == 1
更改為indexPath.item % 5 == 0
),然后在sizeForItemAt
中您可以使用相同的cellForItemAt
的邏輯:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row % 5 == 0{
//return size for addcell ehere
}
if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
return CGSize(width: (((collectionView.frame.width) - 40) / 2), height: 500)
} else { //if iPhone
return CGSize(width: view.frame.width-20 , height: 500)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.