2
顯示圖像爲中心UICollectionViewCell我想表明的,當我在第二項移動居中的圖像爲中心,在UICollectionView第一項顯示圖像,這個項目不居中如何斯威夫特
let itemsPerRow:CGFloat = 1
var sectionInsets = UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)
var productImage = [String]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return productImage.count
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductInfoDetailCell", for: indexPath) as! ProductInfoDetailCell
cell.productLargeImageView.image = UIImage(named: productImage[indexPath.row])
//cell.productLabel.text = productTitle[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let paddingSpace = (sectionInsets.left) * (itemsPerRow + 1)
let availableWidth = collectionView.frame.size.width - paddingSpace
let widthPerItem = availableWidth/itemsPerRow
return CGSize(width: widthPerItem , height: 400)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}
第一圖像,物品是centred-
第二圖像,項不居中
我也使用水平滾動和尋呼UICollectionView
您是否正在使用collectionView上啓用分頁?如果是這樣,您需要使您的collectionivewcell寬度與屏幕寬度一樣大。 – Kingalione