Collection 与 UIPageControl 一起使用时视图高度不正确
Collection view height incorrect when used with UIPageControl
概述
有一个 UICollectionViewCell
包含 imageView。
在 collection 视图上方有一个 UIPageControl
图片从图片库中获取并显示。
问题
- collection 视图中的图像第一次单独超出了上边界。
UIPageControl
加载 collection 视图时未确定高度。
我收到以下错误:
错误:
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.
注意事项:
- 此
UICollectionViewController
作为 child 控制器添加到 UIStackView
。
- 以上问题只是第一次出现,当我从 child view controller 中删除并重新添加时,问题就解决了。
问题:
- 尽管设置了约束并使用了宽高比,为什么图像会溢出边界?
- 如何解决这个问题?
Collection 查看单元格:
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
}
}
从照片库请求资产:
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
==> 高度错误所以设置约束....
==> 此 link 将帮助您解决与高度和约束相关的所有问题
https://www.youtube.com/watch?v=6KImie4ZMwk
感谢您分享您的问题
.
概述
有一个 UICollectionViewCell
包含 imageView。
在 collection 视图上方有一个 UIPageControl
图片从图片库中获取并显示。
问题
- collection 视图中的图像第一次单独超出了上边界。
UIPageControl
加载 collection 视图时未确定高度。我收到以下错误:
错误:
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.
注意事项:
- 此
UICollectionViewController
作为 child 控制器添加到UIStackView
。 - 以上问题只是第一次出现,当我从 child view controller 中删除并重新添加时,问题就解决了。
问题:
- 尽管设置了约束并使用了宽高比,为什么图像会溢出边界?
- 如何解决这个问题?
Collection 查看单元格:
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
}
}
从照片库请求资产:
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
==> 高度错误所以设置约束....
==> 此 link 将帮助您解决与高度和约束相关的所有问题
https://www.youtube.com/watch?v=6KImie4ZMwk
感谢您分享您的问题
.