Apple Interface Builder: adding subview to UIImageView?

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.

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 . ..

Thanks a lot for a very interesting idea (advice). – kpower Jun 3 '10 at 2:18 Thanks! I guess it's like this for the reason that if you put something over your UIImageView, than it's more of a background image than an actual image (as in HTML, you have an tag referring to an actual image, which a screen reader "understands", or you can set the CSS property background-image of another tag, in which case the image is just style, not actual content).

But in the case of iOS, there is no backgroundImage property (just hacks using the backgroundColor) ... so I don't get it (and you can add a subview to UIImageView directly in code as mentioned, so it's just stupid). – krookedking Jul 20 at 16:22 Then it's probably better to create a UIView, put a UIImageView as subview covering all its size and add subviews to the UIView. – Sulthan Oct 3 at 14:11 @Sulthan - That would add more load on the view hierarchy, and you will have to make sure that auto-resizing masks of the main UIView and the UIImageView matches always.

And also we will have to make sure that the UIImageView is always the bottom most view in the view hierarchy! – Raj Oct 4 at 6:43 1 @Raj There are no performance issues. The view hierarchy can handle hundreds of sublayers.

Of course you need to set autoresizing mask, but I don't see that as a problem. You always have to set autoresizing. And bottom most view is not a problem either!

Note that it's still better than creating IBOutlets and setting up UI in code. – Sulthan Oct 4 at 14:35.

Actually, it's not easy to create a subview of UIImageViews in interface builder, but it can be done: Step 1: Create a UIImageView at a top level. Ie. Don't make it the child of any other view.

Double click on this and it'll open up a separate window to represent that image view. With that selected, go to your library and create a UILabel by going to the search field and typing UILabel (not dragging out) You'll see the UILabel created as a child of the UIImageView. Now drag the UIImageView into the correct place.. voila!

Ignore this. Creates children in IB, but not when loaded in the simulator/device. Apologies.

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. 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions