Is there a way to get a collectionViews frame from a collectionviewcell

110 Views Asked by At

I want to use

-preferredLayoutAttributesFittingAttributes inside the UICollectionviewCell subclass we are writing.

Currently we already had a weak pointer to out parentViewController and I try to set the collectionViewcell frame width to be the same as parentViewcontroller.view.frame.size.width.

I don't want to have a pointer to the parentViewController from the CollectionViewCell subclass as having a reference to the parentviewcontroller is a bad idea.

My question is....

Is there a simple and ideal way to get the frame of collectionviewcell's super view from inside the collectionViewcell class?

1

There are 1 best solutions below

5
On BEST ANSWER

If you want to be use the parent view then you don't need any reference. You can do like this:

CGFloat width = self.superview.frame.size.width;

And as @Ralfonso pointed out in the comments:

Also, if you want to get the superview size immediately after being added to a view hierarchy, override didMoveToSuperview: and access the superview property. If it's nil, the view has been removed from the superview. If non-nil, the view has been added to the superview's hierarchy.