Why does my IBDesignable not allow me to use setImage: on my custom UIButton?

In layoutSubviews I call setImage() and pass it to UIImage , but it never appears in Interface Builder, but it always appears when the program starts.

+5
source share
2 answers

You may have downloaded your UIImage using this method:

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

This does not work in the interface builder, because the imageNamed: method uses the main package, but IB loads resources differently. Try something like this:

 - (void)prepareForInterfaceBuilder { UIImage *image = [UIImage imageNamed:@"image" inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil]; [self setImage:image forState:UIControlStateNormal]; } 
+12
source

Go to give a quick answer to anyone who faces this problem, like me.

The problem is that in the Builder interface the different paths to the images are different than in your application.

  let bundle = Bundle(for: self.classForCoder) image = UIImage(named: "your_image.png", in: bundle, compatibleWith: self.traitCollection)! self.setImage(image, for: .normal) 
+1
source

Source: https://habr.com/ru/post/1205636/


All Articles