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)
}
2 回答
使用以下代码行,您可以根据需要以编程方式提供约束
如果你使用故事板作为约束,是的,它将解决你的问题[简单方法]
Try this code-: