ios - UICollectionView 分页视图被切断 - 分页不起作用
问题描述
我正在实现一个需要水平分页的 UICollectionView。UICollectionViewCell 需要与 UICollectionView 的大小相同。该单元格有 2 个嵌入的行。第一个有一个 UITextView 和一个 UIButton,第二个有一个 UITextView。
UICollectionviewCell 和约束在预览中适用于不同尺寸的 iPhone(例如 4s、8、8plus),但是在模拟器中运行时分页无法正常工作,并且我猜单元格的宽度没有居中并且单元格没有移动得不够远,无法到达下一个单元格,从而使单元格看起来被截断。
我已经在这个 url 尝试了解决方案: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。
任何帮助深表感谢。
谢谢
编辑:我添加了我试图用于布局的代码。我可能还缺少其他东西。
解决方案
我能够使用这两个功能进行修复:
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)
}
推荐阅读
- forms - 不返回烧瓶形式
- c# - 如何在反序列化c#中检查对象是否已更改属性
- javascript - 在反应中发出从一个类到另一个类的发送值
- php - 动态获取PHP中最后一个公共数组值
- jquery - postgres中的时间戳比较
- excel - EXCEL:是否有类似 LOOKUP 的函数/方式,但将数组作为输入并返回数组作为结果
- wordpress - 如何解决错误:警告 call_user_func_array() C...\wp-includes\class-wp-hook.php?
- node.js - 从 Firebase Cloud Functions 将数据发送回 Android
- sql - ORACLE:使用具有关系的两个表更新查询
- python - TKinter 几何管理器显示多行小部件