与UIPageControl一起使用时,集合视图高度不正确(Collection view height incorrect when used with UIPageControl)

系统教程 行业动态 更新时间:2024-06-14 16:55:57
与UIPageControl一起使用时,集合视图高度不正确(Collection view height incorrect when used with UIPageControl)

概观

有一个包含UICollectionViewCell 。

在集合视图上方有一个UIPageControl

图像来自照片库并显示。

问题

第一次单独收集视图中的图像超出顶部边界。

加载集合视图时UIPageControl高度。

我收到以下错误:

错误:

the behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

Error:

the behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

Points to note:

This UICollectionViewController is added as a child controller to UIStackView. The above problem only occurs for the first time, when I remove from child view controller and add again, the issue is resolved.

Question:

Why is the image overflowing it's boundaries in spite of setting constraints and using aspect fit ? How to fix this issue ?

Collection View Cell:

class PhotoBrowserCell : UICollectionViewCell { let imageView = UIImageView() //MARK: Initializers required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } override init(frame: CGRect) { super.init(frame: frame) setup() } //MARK: Setup private func setup() { setupBaseView() setupImageView() } private func setupBaseView() { backgroundColor = UIColor.blueColor() } private func setupImageView() { imageView.image = UIImage(named: "PlaceHolder") imageView.contentMode = .ScaleAspectFit imageView.clipsToBounds = true imageView.setContentHuggingPriority(1000, forAxis: .Vertical) contentView.addSubview(imageView) imageView.translatesAutoresizingMaskIntoConstraints = false let insets = UIEdgeInsets(top: 10, left: 20, bottom: 30, right: 20) imageView.leadingAnchor.constraintEqualToAnchor(contentView.leadingAnchor, constant: insets.left).active = true imageView.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor, constant: -insets.right).active = true imageView.topAnchor.constraintEqualToAnchor(contentView.topAnchor, constant: insets.top).active = true imageView.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor, constant: -insets.bottom).active = true } }

Request Asset from photo library:

private func requestAssetSelected() { let size = imageViewSize if let assetSelected = assetSelected { cachingManager.requestImageForAsset(assetSelected, targetSize: size, contentMode: .AspectFit, options: nil) { [weak self] image, info in //This completion handler might be called multiple times //First time it provides a temporary image and then shows a full size image //So ensure it is not a temporary (degraded) image if let image = image, isDegraded = info?[PHImageResultIsDegradedKey] as? Int where isDegraded == 0 { self?.imagesToUpload.append(image) self?.photoBrowser.reload() } } } else { photoBrowser.reload() } }

最满意答案

根本原因:

集合视图单元格高度计算错误 单元格高度错误,因为集合视图高度错误 集合视图高度错误,因为在填充集合视图时UIPageControl为零。 很久以后,计算了UIPageControl高度。 UIPageControl正在使用它的内在大小。

这就是为什么它只是第一次发生的原因。

解:

添加高度约束,以便它不必依赖于它的内在高度计算:

pageControl.heightAnchor.constraintEqualToConstant(38).active = true

Root Cause:

The collection view cell height calculation was wrong Cell height was wrong because the collection view height was wrong Collection view height was wrong because the UIPageControl was zero while collection view was populated. And much later the UIPageControl height was computed. UIPageControl was using it's intrinsic size.

This is the reason why it only happened the first time.

Solution:

Add a height constraint, so that it doesn't have to depend on it's intrinsic height calculation:

pageControl.heightAnchor.constraintEqualToConstant(38).active = true

更多推荐

本文发布于:2023-04-10 11:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4013975905a2611e890d3a5d4fc581dc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   不正确   高度   UIPageControl   incorrect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!