0

I am trying to set the frame size of an UIImageView inside a subclass of UICollectionCell but it does not work. I use storyboard.

In the code, everything works but the last line regarding the imageView. If i put the code in drawRect it works ok.

- (id)initWithCoder:(NSCoder *)decoder {
if ((self = [super initWithCoder:decoder]))
{
    self.backgroundColor = [UIColor colorWithWhite:0.85f alpha:1.0f];

    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.borderWidth = 1.0f;

    self.layer.cornerRadius = 8.0f;
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowRadius = 3.0f;
    self.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
    self.layer.shadowOpacity = 0.5f;

    self.imageView.frame = CGRectMake(10, 10, 50, 50);
}
Sucrenoir
  • 2,994
  • 1
  • 27
  • 31

2 Answers2

1

That is because your imageView (I believe it's and IBOutlet) is not yet initialized, it's still nil. Be aware that some of you variables/properties are being initialized inside init's method. Set you breakpoint and you will see. Try set the imageView values somewhere else such as drawRect (as you suggest) or awakeFromNib.

Ohmy
  • 2,201
  • 21
  • 24
0

Is the image view being moved later by a different object? Try moving that code into layoutSubviews

Add this method:

- (void) layoutSubviews
{
    [super layoutSubviews];

    self.imageView.frame = CGRectMake(10, 10, 50, 50);
}
SomeGuy
  • 9,670
  • 3
  • 32
  • 35
  • No it does not work in layoutSubviews. The view is not being moved later... I resolved the problem by calling the method in drawRect (not optimal, but it works) – Sucrenoir Oct 02 '13 at 12:07