我在另一个 View 的容器 View 内有一个 CollectionView (子类 CollectionViewController)。 CollectionViewCells 在 Interface Builder 中定义,并包含一个带有更多 subview 的 UIView。
我的收藏 View 包含 7 个部分,每个部分 1 个项目。这些部分具有定义的插入和大小(通过 CollectionViewDelegateFlowLayout)。每个单元格应填充 Collection View 的大小,没有插图。
问题似乎是单元格内的 View 未正确渲染。使用约束将其设置为固定在每一侧,但这似乎不起作用。
我尝试记录单元格本身与单元格内部 View 的宽度和高度。当我滚动 collectionView 时,所有奇怪的单元格显示的内容大小都远小于单元格的大小。这也适用于第一个偶数单元格,但仅限于我滚动到第二个偶数单元格。由于这种大小差异,单元格的 subview 根本不会渲染。ui
我的 IB 设置:
CollectionViewController:
// MARK: CollectionViewController
class DayViewController: UICollectionViewController {
// MARK: Properties
private let reuseIdentifier = "DayCell"
// MARK: CollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 7
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! DayViewCell
NSLog("%f — %f", cell.bounds.width, cell.content.bounds.width)
return cell
}
}
// MARK: CollectionViewDelegateFlowLayout
extension DayViewController : UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// return CGSize(width: collectionView.bounds.width - 40.0, height: collectionView.bounds.height - 40.0)
return CGSize(width: collectionView.bounds.width - 40.0, height: collectionView.bounds.height - 40.0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)
}
}
CollectionViewCell:
class DayViewCell: UICollectionViewCell {
override var bounds: CGRect {
didSet {
contentView.frame = bounds
}
}
@IBOutlet weak var content: UIView!
@IBOutlet weak var progress: UIView!
@IBOutlet weak var rank: UILabel!
@IBOutlet weak var remainingRep: UILabel!
override func drawRect(rect: CGRect) {
super.drawRect(rect)
layer.cornerRadius = 5
layer.shadowOffset = CGSize(width: 0, height: 2)
layer.shadowOpacity = 0.5
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowRadius = 5
layer.masksToBounds = false
clipsToBounds = false
}
override func layoutSubviews() {
super.layoutSubviews()
content.layer.cornerRadius = 5
content.clipsToBounds = true
progress.frame = CGRect(x: 0, y: 0, width: 100, height: 22)
}
}
运行:(第一次渲染上的奇数单元格/偶数单元格/第二次渲染上的偶数单元格)
最佳答案
感谢您提供来源。一些想法:
看起来您正在搞乱自动布局大小类别。我不确定你是故意这样做的。
选择容器 View ,然后选择属性检查器。在底部您会看到一些复选框。这表明您仅为某些尺寸类别设置了自动布局设置。
进入文件检查器并禁用大小类别。
您会发现事情开始起作用。
如果您仅针对 iPhone(而非 iPad)进行纵向开发,您可能应该禁用尺寸类别。如果您想针对其他事情进行设计,研究尺寸类别可能会有所帮助。
关于ios - UICollectionViewCell subview 的大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35352324/