I created UIImageView
with the help of Interface Bulder. Now I want to place label inside it (as its subview). In code I can type something like: [myUIImageView addSubview:myUILabel];
But can I do it with the help of IB? I found the solution for UIView
, but can't find something similar for UIImageView
.

- 3,871
- 4
- 42
- 62
4 Answers
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.

- 20,030
- 7
- 43
- 238

- 7,390
- 10
- 60
- 92
-
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 '11 at 16:22
-
7Then 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 03 '11 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 Pawan Gumdal Oct 04 '11 at 06:43
-
6@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 04 '11 at 14:35
-
1Yup, agreed! This would also be a solution. – Raj Pawan Gumdal Oct 04 '11 at 16:12
-
I also not being able to add subview in UIImageView from Interface Builder !!! Programmatically I could do it. – NSPratik Jul 23 '15 at 14:21
-
This is an edge case but if you are reliant on sizing your imageView by calling `sizeToFit`, this method will not work. No idea why. – rigdonmr Jun 29 '16 at 03:29
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:
Here is the result:
Don't forget to set background as clear color
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.
-
2The question really was: can I add direct subview of UIImageView with IB? And the answer was no. That's why it was marked as correct. I mean, I knew about both of your "substitutions" and was interested not in "any way to do this", but in possibility of this particular one. But you idea is much better, so upvote! – kpower Oct 14 '12 at 04:25
-
2Ah ya it doesn't exactly answer your question. But it seems to be the way it's supposed to be. Thanks for upvote. – user4951 Oct 14 '12 at 04:33
-
1UIView is supposed to contain other views. UIImageView is supposed to contain picture and that's it. So if you want a subview inside a UIImageView, the way to do it would be to have a transparent UIView in front of the UIImageView. You can do this the checked way but I am sure apple must have intended to be this way – user4951 Jan 19 '17 at 10:33
In latest XCode(4.5) there is an option to drag and drop the required controls to the parent.
It is quite easy.
Attached screen shot for the same. I dragged the Label/TextField and Button to UIImageView

- 315
- 1
- 4
- 16
-
++10 Nirav - Wow - DUH, OF COURSE ! ... your simple technique saved me an entire bottle of Excedrin Extra Strength ! – Howard Pautz Mar 23 '13 at 00:23
-
7I don't get it. Xcode 4.6.2 will not let me drag it into the `UIImageView`, whether in the canvas, nor in the outline. In the outline, I start with the `UIImageView` and below at the same level, the `UILabel`. I left-click on the label and drag it up. Xcode shows a line where the `UILabel` will go if I release. Clearly, that line never gets indented: its stays at the root level. When I release, the `UILabel` may swap place with the `UIImageView`, but never gets *inside* it (indented below it). I tried a few modifiers (shift, option), without success. What did I miss? – Jean-Denis Muys Apr 25 '13 at 09:34
-
3You can't drag an `UIView` inside an `UIImageView` in the `Interface Builder` not even in `Xcode 8`. – Iulian Onofrei Oct 04 '16 at 07:17
-
It's XCode 9 now, and you still won't be able to drag and drop UIView into a UIImageView in IB. – Anjan Biswas Nov 02 '17 at 23:16
Use this code:
UIImage *image = [UIImage imageNamed:@"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view insertSubview:imageView atIndex:0];
(replace background.png with image) (replace atIndex:0 with whatever place in the .index root you want to insert the image and your off.

- 9
- 1