【发布时间】:2017-12-29 13:35:15
【问题描述】:
iOS 新手在这里。我正在使用带有许多单元格的集合视图控制器,并且当我滚动到底部时,底部的一些单元格被切断(不显示)。
我尝试实现UICollectionViewDelegateFlowLayout 协议。
我也试过设置
self.view.frame.size.height。但是,我只能将self.view.frame.size.height 设置为较小的值,例如 200,但是当我尝试超过默认大小时,屏幕不会变长。
我是否缺少默认约束?自动布局也能解决问题吗?
谢谢!
编辑:
我尝试添加约束 - 比如做 self.view.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 8).isActive = true 但不确定“equalTo”的参数值应该是什么。我的目标是在视图控制器的主视图中添加约束。
这是我的代码 - 在此先感谢!
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// self.clearsSelectionOnViewWillAppear = false
self.view.frame.size.height = 1500
// Register cell classes
self.myCollectionView!.register(TableCell.self, forCellWithReuseIdentifier: "tableCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 8
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableCell", for: indexPath) as! TableCell
cell.contentView.backgroundColor = UIColor.blue
return cell
}
}
class TableCell : UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
【问题讨论】:
-
检查集合视图的约束。如果集合视图覆盖完整的控制器高度和宽度,则为集合视图提供前导、尾随、顶部、底部约束。如果您嵌入了标签栏或导航栏以及根据管理框架并给出相同的约束。
-
如何通过顶部的导航栏添加约束?
-
您从情节提要或以编程方式给出约束?
-
以编程方式
-
显示。您的。代码。您不对集合视图的单元格使用约束,因此听起来您做错了什么。解释集合视图如何进入界面。您没有提供任何可以让任何人帮助您的信息。
标签: ios swift uicollectionview