Too late to answer this question, but I recently survived this question.
@lee above answer help me a lot to get my answer. But this answer is limited to objective-c , and I worked in quickly .
@lee Regarding your answer, let me let a fast user solve this problem easily. To do this, follow these steps:
Declare a CGFloat variable in the declaration section:
var height : CGFloat!
In viewDidAppear you can get it:
height = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height
Maybe when you reload data, then you need to calculate the new height with the new data, then you can get it: addObserver to listen when your CollectionView finished reloading the data in viewWillAppear :
override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) .... .... self.shapeCollectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old, context: nil) }
Then add the following function to get a new height or to do something after CollectionView reboot is completed:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { let newHeight : CGFloat = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height var frame : CGRect! = self.myCollectionView.frame frame.size.height = newHeight self.myCollectionView.frame = frame }
And do not forget to remove the observer:
override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(true) .... .... self.myCollectionView.removeObserver(self, forKeyPath: "contentSize") }
I hope this helps you quickly solve your problem.
Er. Vihar Apr 19 '17 at 5:38 on 2017-04-19 05:38
source share