leanne
leanne

Reputation: 8729

Retrieve constraint values within collection view cell?

How can I programmatically access a storyboard-set constraint on a prototype cell for a collection view?

I'm working within the UICollectionViewDelegateFlowLayout's collectionView(_:layout:sizeForItemAtIndexPath:) method.

Collection cell setup, including constraints

Specifically, I want the aspect ratio set up for the Image View.

The cell isn't available yet at this point, and the constraint isn't present in the collection view's constraints array. (Edit for clarification: I can't access the cell's content, and since the constraints are associated with the cell, they're not present in the collection view's constraints array.)

I can't set an outlet, as the cell is repeating content.

Other than creating a cell or layout subclass, how can I access this constraint?

I'm using Xcode 7.3 with Swift 2.2.

Upvotes: 0

Views: 177

Answers (1)

leanne
leanne

Reputation: 8729

This looks like the same issue specified in this question: Xcode 7 beta 5, Swift 2: UITableViewCell's subviews are not added to the contentView at runtime

The contentView's subviews aren't available unless programmatically added in addition to the storyboard's customization. This problem exists even with a custom cell class.

The solution in that question (making sure everything is marked as installed in the storyboard) didn't work for me, so, for the time being at least, I'll be using a hardcoded aspect ratio.

Upvotes: 0

Related Questions