Swift - 水平居中 UICollectionView 单元格不起作用
Swift - Center Horizontally UICollectionView Cell doesn't work
这是 CollectionView ViewController 中的代码,但它不会使 Cells 位于 CollectionView 的中心,结果如下:
extension DeviceAndEquipmentViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView === deviceCollectionView {
guard selectedDeviceList.count != 0 else {
selectedDeviceList.insert("nil", at: 0)
return 1
}
return selectedDeviceList.count
} else if collectionView === equipmentCollectionView {
guard selectedEquipmentList.count != 0 else {
selectedEquipmentList.insert("nil", at: 0)
return 1
}
return selectedEquipmentList.count
} else if collectionView === softwareCollectionView {
guard selectedSoftwareList.count != 0 else {
selectedSoftwareList.insert("nil", at: 0)
return 1
}
return selectedSoftwareList.count
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let inset:CGFloat = 10
return UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfItemsPerRow:CGFloat = 1
let spacingBetweenCells:CGFloat = 16
let totalSpacing = (2 * self.spacing) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)
let width = (deviceCollectionView.bounds.width - totalSpacing) / numberOfItemsPerRow
if collectionView == deviceCollectionView {
let device = self.selectedDeviceList[indexPath.row]
if device != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
} else if collectionView == equipmentCollectionView {
let equipment = self.selectedEquipmentList[indexPath.row]
if equipment != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
} else {
let software = self.selectedSoftwareList[indexPath.row]
if software != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
}
}
}
这是每个 CollectionView FlowLayout 的设置
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
layout.minimumLineSpacing = spacing
layout.minimumInteritemSpacing = spacing
self.equipmentCollectionView?.collectionViewLayout = layout
我看过其他主题,但没有解决我的问题,我为每个集合视图单元格使用 XIB,并没有在代码中完成所有工作,如果你能帮我解决这个问题,我将不胜感激。
我找到了答案,但我仍然不知道它在其他 ViewController 但不是这个,这 ViewController 和其他的唯一区别是在这一个中,我使用了超过 1 个 ViewController。我在下面链接了答案,所以如果有人遇到这个问题可以解决它,我使用了第二种解决方案并且它有效。感谢 Cœur 的分享。
这是 CollectionView ViewController 中的代码,但它不会使 Cells 位于 CollectionView 的中心,结果如下:
extension DeviceAndEquipmentViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView === deviceCollectionView {
guard selectedDeviceList.count != 0 else {
selectedDeviceList.insert("nil", at: 0)
return 1
}
return selectedDeviceList.count
} else if collectionView === equipmentCollectionView {
guard selectedEquipmentList.count != 0 else {
selectedEquipmentList.insert("nil", at: 0)
return 1
}
return selectedEquipmentList.count
} else if collectionView === softwareCollectionView {
guard selectedSoftwareList.count != 0 else {
selectedSoftwareList.insert("nil", at: 0)
return 1
}
return selectedSoftwareList.count
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let inset:CGFloat = 10
return UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfItemsPerRow:CGFloat = 1
let spacingBetweenCells:CGFloat = 16
let totalSpacing = (2 * self.spacing) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)
let width = (deviceCollectionView.bounds.width - totalSpacing) / numberOfItemsPerRow
if collectionView == deviceCollectionView {
let device = self.selectedDeviceList[indexPath.row]
if device != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
} else if collectionView == equipmentCollectionView {
let equipment = self.selectedEquipmentList[indexPath.row]
if equipment != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
} else {
let software = self.selectedSoftwareList[indexPath.row]
if software != "nil" {
return CGSize(width: width, height: 45)
} else {
return CGSize(width: width + 15, height: 55)
}
}
}
}
这是每个 CollectionView FlowLayout 的设置
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
layout.minimumLineSpacing = spacing
layout.minimumInteritemSpacing = spacing
self.equipmentCollectionView?.collectionViewLayout = layout
我看过其他主题,但没有解决我的问题,我为每个集合视图单元格使用 XIB,并没有在代码中完成所有工作,如果你能帮我解决这个问题,我将不胜感激。
我找到了答案,但我仍然不知道它在其他 ViewController 但不是这个,这 ViewController 和其他的唯一区别是在这一个中,我使用了超过 1 个 ViewController。我在下面链接了答案,所以如果有人遇到这个问题可以解决它,我使用了第二种解决方案并且它有效。感谢 Cœur 的分享。