8

In layoutSubviews I'm calling setImage() and pass it a UIImage but it never shows up in Interface Builder, but it always shows up when I run the program.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

2 Answers2

15

Probably you are loading your UIImage with this method:

UIImage *image = [UIImage imageNamed:@"image"];

This does not work in interface builder because imageNamed: method uses main bundle but IB loads resources in different way. Try something like this:

- (void)prepareForInterfaceBuilder
{
    UIImage *image = [UIImage imageNamed:@"image" inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
    [self setImage:image forState:UIControlStateNormal];
}
Leszek Szary
  • 9,763
  • 4
  • 55
  • 62
5

Going to give the swift answer for anyone who comes across this problem like I did.

The problem is that paths to images are different in Interface Builder than they are in your app.

 let bundle = Bundle(for: self.classForCoder)
 image = UIImage(named: "your_image.png", in: bundle, compatibleWith: self.traitCollection)!
 self.setImage(image, for: .normal) 
A. Maly
  • 305
  • 5
  • 8