【发布时间】:2019-05-08 07:47:34
【问题描述】:
我正在实现一个需要水平分页的 UICollectionView。 UICollectionViewCell 需要与 UICollectionView 的大小相同。该单元格有 2 个嵌入的行。第一个有一个 UITextView 和一个 UIButton,第二个有一个 UITextView。
UICollectionviewCell 和约束在预览中适用于不同尺寸的 iPhone(例如 4s、8、8plus),但是在模拟器中运行时分页无法正常工作,并且我猜单元格的宽度没有居中并且单元格移动得不够远,无法到达下一个单元格,从而使单元格看起来被截断。
我已经在这个网址尝试了解决方案:UICollectionView Horizontal Paging not centered
但我似乎无法获得正确的宽度和居中。我正在使用的代码如下。
internal class AccessViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PagerContentCollectionViewCell
//Set the cell title
cell.txtTitle.text = "title"
//add a target for the call capability
cell.btnCall.addTarget(self, action: #selector(didButtonClick), for: .touchUpInside)
//description
cell.txtDescription.text = "description"
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//let i = IndexPath(item: titles.count, section: 0)
collectionView.scrollToItem(at: indexPath, at: .right, animated: true)
print("Selected")
collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let frameSize = collectionView.frame.size
return CGSize(width: frameSize.width, height: frameSize.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let frameSize = collectionView.frame.size
return CGSize(width: frameSize.width, height: frameSize.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}
UICollectionView 的宽度和高度分别为 267、153。 UICollectioViewCell 宽高 262, 150 Section Insets 为 0,0,0,0。
非常感谢任何帮助。
谢谢
编辑:我添加了我试图用于布局的代码。我可能还缺少其他东西。
【问题讨论】:
-
尝试给出全宽并测试它的外观
-
我试过了,但没有成功。
-
这里没有布局代码。
-
我添加了我尝试过的其他代码。
标签: ios pagination uicollectionview uicollectionviewcell horizontal-scrolling