UICollectionViewCell 子视图的大小不正确
UICollectionViewCell subviews not sized correctly
我在另一个视图的容器视图中有一个 CollectionView(子类 CollectionViewController)。 CollectionViewCells 在 Interface Builder 中定义并包含一个带有更多子视图的 UIView。
我的 collection 视图包含 7 个部分,每个部分有 1 个项目。这些部分具有定义的插图和大小(通过 CollectionViewDelegateFlowLayout)。每个单元格应填充 collection 视图的大小,没有插图。
问题似乎是单元格内的视图未正确呈现。使用约束将其设置为固定在每一侧,但这似乎不起作用。
我尝试记录单元格本身的宽度和高度与单元格内部视图的对比。当我滚动浏览 collection 视图时,所有奇数单元格显示内容的大小远小于单元格的大小。这也适用于第一个偶数单元格,但仅在我滚动到第二个偶数单元格之前。由于这种大小差异,单元格的子视图不会在 all.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)
}
}
运行:(第一次渲染时的奇数单元格/偶数单元格/第二次渲染时的偶数单元格)
您提到 UICollectionViewController
在容器视图中。它是否设置正确,以便获得所有预期的 UIViewController
调用,例如 viewWillAppear()
?
如果您可以 post 演示问题的示例项目,我很乐意尝试调试它。
关于您在此处post编写的代码的一些想法:
在 DayViewCell 代码中,覆盖是非常不寻常的
bounds
设置框架。这让我很担心。
drawRect()
不是设置阴影和角的地方
半径。这应该在 init(frame:)
and/or init?(coder:)
中完成。
在 drawRect()
中执行此操作很昂贵并且可能会影响您的帧速率
滚动时。
我也会把你在 layoutSubviews()
中的所有东西都移到
init
方法。 运行 编码每个
没有任何优势
布局视图的时间。
感谢您提供资源。一些想法:
您好像在搞乱自动布局大小 类。我不确定你是故意这样做的。
Select 容器视图,然后 select 属性检查器。在底部,您会看到一些复选框。这表明您只为某些尺寸设置了自动布局设置 类。
进入文件检查器并禁用大小 类。
您会发现事情开始起作用了。
如果您只是针对 iPhone(而不是 iPad)开发并且只是纵向,您可能应该只禁用尺寸 类。如果你想为那些其他的东西设计,它可能有助于研究尺寸 类。
我在另一个视图的容器视图中有一个 CollectionView(子类 CollectionViewController)。 CollectionViewCells 在 Interface Builder 中定义并包含一个带有更多子视图的 UIView。
我的 collection 视图包含 7 个部分,每个部分有 1 个项目。这些部分具有定义的插图和大小(通过 CollectionViewDelegateFlowLayout)。每个单元格应填充 collection 视图的大小,没有插图。
问题似乎是单元格内的视图未正确呈现。使用约束将其设置为固定在每一侧,但这似乎不起作用。
我尝试记录单元格本身的宽度和高度与单元格内部视图的对比。当我滚动浏览 collection 视图时,所有奇数单元格显示内容的大小远小于单元格的大小。这也适用于第一个偶数单元格,但仅在我滚动到第二个偶数单元格之前。由于这种大小差异,单元格的子视图不会在 all.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)
}
}
运行:(第一次渲染时的奇数单元格/偶数单元格/第二次渲染时的偶数单元格)
您提到 UICollectionViewController
在容器视图中。它是否设置正确,以便获得所有预期的 UIViewController
调用,例如 viewWillAppear()
?
如果您可以 post 演示问题的示例项目,我很乐意尝试调试它。
关于您在此处post编写的代码的一些想法:
在 DayViewCell 代码中,覆盖是非常不寻常的
bounds
设置框架。这让我很担心。drawRect()
不是设置阴影和角的地方
半径。这应该在init(frame:)
and/orinit?(coder:)
中完成。 在drawRect()
中执行此操作很昂贵并且可能会影响您的帧速率 滚动时。我也会把你在
layoutSubviews()
中的所有东西都移到
init
方法。 运行 编码每个
没有任何优势 布局视图的时间。
感谢您提供资源。一些想法:
您好像在搞乱自动布局大小 类。我不确定你是故意这样做的。
Select 容器视图,然后 select 属性检查器。在底部,您会看到一些复选框。这表明您只为某些尺寸设置了自动布局设置 类。
进入文件检查器并禁用大小 类。
您会发现事情开始起作用了。
如果您只是针对 iPhone(而不是 iPad)开发并且只是纵向,您可能应该只禁用尺寸 类。如果你想为那些其他的东西设计,它可能有助于研究尺寸 类。