cridgit
cridgit

Reputation: 183

Xcode6 Interface Builder constraints not stretching subviews

Xcode Interface Builder is driving me nuts and I've spent most of my day googling for a solution.

I have a storyboard with a CollectionViewCell that contains a UIImageView. The CollectionViewCell size is 125 x 125 and Autoresize subviews is checked.

The UIImageView has 4 constraints to its superview (leading, trailing, top, bottom), all set to 0. This should ensure that the UIImageView is sized to fill the superview. The UIImageView is sized correctly when the CollectionViewCell is shrunk to a smaller size, but IT DOES NOT WORK when the CollectionViewCell is stretched to a larger size.

EDIT: I've tried the same using a UILabel subview and the same thing happens.

Is this a known problem or is there any way to debug constraints?

Thanks

Upvotes: 2

Views: 830

Answers (2)

cridgit
cridgit

Reputation: 183

I've finally found a complete solution to the problem from http://www.nomtek.com/adjusting-your-app-to-ios-8/

Firstly, I had to ensure my CollectionViewCell had Autoresize Subviews checked in Interface Builder, then I had to add the following awakeFromNib method on the ConnectionViewCell subclass.

- (void)awakeFromNib {

    [super awakeFromNib];

    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

With these two things, the contents of my cells (UIImageView and UILabel) are stretched properly even with dynamic cell sizing using sizeForItemAtIndexPath.

Upvotes: 1

cridgit
cridgit

Reputation: 183

I've made partial progress.

I was setting the cell size dynamically using sizeForItemAtIndexPath.

If I drop that method and set a larger Cell Size of the CollectionView's FlowLayout in Interface Builder, it works as expected. This means the constraints and subview size are updated at compile time.

I would still love to know how to dynamically update the constraints and subview sizes when using sizeForItemAtIndexPath. Any suggestions?

Upvotes: 0

Related Questions