动态改变CollectionView的高度以适应内容大小时的错误结果 [英] Wrong result when dynamically changing the height of the CollectionView to adapt to content size
问题描述
在我的 DetailViewController
中,我有两个 collectionViews
并且我正在尝试动态更改它们的高度.我可以通过使用 DetailViewController
中的观察者更改两个高度约束 IBOutlets
来成功实现这一点.但是,如果我从 DetailViewController
推送另一个 ViewController 然后返回,则它不起作用.
In my DetailViewController
, I have two collectionViews
and I am trying to change their height dynamically. I can successfully achieve this by changing the two heigh constraints IBOutlets
with observers in DetailViewController
. However, it does not work if, from DetailViewController
I push another ViewController and then I go back.
这是DetailViewController"中的代码
This is the code in "DetailViewController"
@IBOutlet weak var deadlineCollectionViewHeight: NSLayoutConstraint!
@IBOutlet weak var tasksCollectionViewHeight: NSLayoutConstraint!
private var CVcontentSizeObservation: [NSKeyValueObservation]?
private var initialCollectionViewsHeight: InitialCollectionViewsHeight?
override func viewDidLoad() {
super.viewDidLoad()
initialCollectionViewsHeight = InitialCollectionViewsHeight(deadlines:
deadlineCollectionViewHeight.constant, tasks: tasksCollectionViewHeight.constant)
observeCollectionView()
}
private func observeCollectionView(){
CVcontentSizeObservation = [
deadlinesCollectionView.observe(\.contentSize, options: .new, changeHandler: {[weak self] (cv, _) in
guard let self = self else { return }
if cv.collectionViewLayout.collectionViewContentSize.height < self.initialCollectionViewsHeight!.deadlines{
self.deadlineCollectionViewHeight.constant = cv.collectionViewLayout.collectionViewContentSize.height
}
}),
tasksCollectionView.observe(\.contentSize, options: .new, changeHandler: {[weak self] (cv, _) in
guard let self = self else { return }
if cv.collectionViewLayout.collectionViewContentSize.height < self.initialCollectionViewsHeight!.tasks{
self.tasksCollectionViewHeight.constant = cv.collectionViewLayout.collectionViewContentSize.height
}
})
]
}
private struct InitialCollectionViewsHeight{
var deadlines: CGFloat
var tasks: CGFloat
}
问题
如果我从主 VC 推送 DetailViewController
,这可以正常工作,但是如果我从那里推送另一个 VC 然后返回,我会得到错误的结果.
This works fine if from the home VC I push the DetailViewController
, but if from there I push another VC and then go back, I get a wrong result.
第一张图片显示了最初推送 DetailViewController
后的正确行为.但是,如果我按下编辑按钮"按钮并按下一个新的 VC,我在关闭它并从堆栈中弹出 VC 后得到错误的结果(即:我回到 DetailViewController
),如第二张图片所示
The first image shows the correct behavior after the DetailViewController
is initially pushed. However, if I press on the "Edit Button" button and a new VC is pushed, I get a wrong result after I dismiss it and pop the VC from the stack (i.e: I go back to DetailViewController
), as seen in the second image
推荐答案
据我所知,您正在尝试使集合视图适合它们的高度.由于 UICollectionViewLayout 行为,使用约束方法可能会带来一些问题.
From what I understand, you are trying to make collection views to fit their height. Using constraint approach may bring some problems, due to UICollectionViewLayout behaviour.
我建议使用这种方法:
class AutoSizingCollectionView : UICollectionView
{
override func layoutSubviews() {
super.layoutSubviews()
if (self.bounds.size.equalTo(self.intrinsicContentSize)) {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize
{
return self.collectionViewLayout.collectionViewContentSize
}
}
用 AutoSizingCollectionView
类交换你的 CollectionViews,它们会使用 IntrinsicContentSize
方法自动将它们的高度缩放到它们的内容大小.它将允许您轻松地将底部集合视图固定到第一个视图的底部.
Exchange your CollectionViews with AutoSizingCollectionView
class and they will scale their height to their content size automatically using IntrinsicContentSize
method. It will allow you to easily pin bottom collection view to bottom of the first one.
我希望能解决您的问题.
I hope that solves root of your problem.
这篇关于动态改变CollectionView的高度以适应内容大小时的错误结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!