Apple Interface Builder: adding subview to UIImageView

I created a UIImageView using Interface Bulder. Now I want to place a shortcut in it (as its subtitle). In the code, I can type something like: [myUIImageView addSubview:myUILabel]; But can I do this with IB? I found a solution for UIView but cannot find something similar for UIImageView .

+49
interface-builder subview uiimageview addsubview
Mar 10 '10 at 9:00
source share
4 answers

You cannot add subview to UIImageView in the interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then the overhead of installing autoresist masks and subquery placements must be handled in code, which is cumbersome.

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

Thus, you can add a subview to your instances of “now UIImageView” in the interface builder, which is very simple.

Hope this helps.

+104
Jun 02 '10 at 10:20
source share

I would like to add an answer.

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

Then put the UIImageView BEFORE that.

Thus, the UIView has subzones, and the UIImageView is the background of the UIView .

I think the intention of the apple.

Here is a screenshot:

enter image description here

Here is the result:

enter image description here

Remember to set the background as pure color.

enter image description here

Now, if someone could really point me to a tutorial on how to do this, that would be great. I spent several hours on this, checking the answer. The verified answer is a great answer, but very unintuitive because you cannot clearly see your background image while you work. I believe this is the right way to do this.

+12
Oct. 12
source share

The latest Xcode (4.5) has the ability to drag and drop the necessary controls to the parent element.

This is pretty easy. Attached screenshot for him. I dragged Label / TextField and Button to UIImageView enter image description here

0
Nov 16 '12 at 6:45
source share

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 the image) (replace atIndex: 0 with the location where the .index root is located, you want to insert the image and off.

-four
Mar 03 '13 at 19:16
source share



All Articles