When is the UICollectionView done loading completely?

Its Late to answer,
At viewDidAppear you can get it by:

float height = self.myCollectionView.collectionViewLayout.collectionViewContentSize.height;

Maybe when you reload data then need to calculate a new height with new data then you can get it by: add observer to listen when your CollectionView finished reload data at viewdidload:

[self.myCollectionView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld context:NULL];

Then add bellow function to get new height or do anything after collectionview finished reload:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary  *)change context:(void *)context
{
    //Whatever you do here when the reloadData finished
    float newHeight = self.myCollectionView.collectionViewLayout.collectionViewContentSize.height;    
}

And don't forget to remove observer:
Write the code in viewWillDisappear

[self.myCollectionView removeObserver:self forKeyPath:@"contentSize" context:NULL];


For more information please look into the answer https://stackoverflow.com/a/28378625/6325474


SWIFT 3: version of @libec's answer

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    collectionView.removeObserver(self, forKeyPath: "contentSize")
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let observedObject = object as? UICollectionView, observedObject == collectionView {
        print("done loading collectionView")
    }
}

If you wanna go with the approach from the question you linked, then:

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView?.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old, context: nil)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if let observedObject = object as? UICollectionView where observedObject == self.collectionView {
        print(change)
    }
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.collectionView?.removeObserver(self, forKeyPath: "contentSize")
}

Tags:

Swift