2

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

cridgit
  • 183
  • 8
  • Try calling sizeToFit on your imageview when the cell resizes (I.e. In layoutSubviews) – Anna Dickinson Oct 16 '14 at 13:55
  • Hi Anna, I have tried this (see code snippet below) but unfortunately still does't work. `@implementation DeckViewCell - (void)layoutSubviews { [super layoutSubviews]; [self.thumbnail sizeToFit]; [self.title sizeToFit]; }` – cridgit Oct 17 '14 at 10:17

2 Answers2

1

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.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
cridgit
  • 183
  • 8
0

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?

cridgit
  • 183
  • 8
  • As I've been learning autolayout, I've found it works best if you let it do as much as possible. Any time you set something manually, it just kinda gives up and the results become unpredictable. But, of course, some things must be done programmaticly. In those case, try setting sizes by modifying constraints: for example, create height and width constraints on something you need to resize, and modify them during updateConstraints in your view controller or a custom view class. (a vague answer, but more things to experiment with!) – Anna Dickinson Oct 17 '14 at 13:42
  • Oh, and make sure you set translatesautoresizingmaskintoconstraints to false for everything. – Anna Dickinson Oct 17 '14 at 13:44