Apple Interface Builder: adding subview to UIImageView

Solution 1:

You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome.

So there is an easy workaround. Instead of dragging an instance of UIImageView in the nib, just drag a UIView and change its class to UIImageView from UIView (cmd+4 option of inspector). The only difference you find in the nib for default imageView instance and your new UIImageView subclass instance is: you cannot set image to your new imageView from nib (cmd+1 option). So, in the -viewDidLoad method of its appropriate viewController, set image to this outlet of UIImageView.

By doing so, you are free to add subviews to your "now UIImageView" instances in interface builder, which is much easy.

Hope this helps . . .

Solution 2:

I would like to add answer.

While it sucks you cannot add subview to UIImageView, you can get the same effect by incorporating UIView with transparent (clear color) background.

Then put the UIImageview BEFORE it.

So the UIView has the subviews and the UIImageview is the background of the UIView.

I think that's apple's intent.

Here is a screenshot:

enter image description here

Here is the result:

enter image description here

Don't forget to set background as clear color

enter image description here

Now if someone could actually point me to a tutorial how to do this it'll be great. I spent hours doing it the checked answered way. The checked answer is a fine answer but very unintuitive because you can't see your background image clearly while working. I think mine is the proper way to do so.