Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Interface Builder: adding subview to UIImageView

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.

like image 751
kpower Avatar asked Mar 10 '10 09:03

kpower


People also ask

What is subclass of UIView?

The UIView class is a key subclassing point for visual content that also requires user interactions. Although there are many good reasons to subclass UIView , it is recommended that you do so only when the basic UIView class or the standard system views do not provide the capabilities that you need.

What is UIImageView?

An object that manages image data in your app.

How do I install UIImageView?

First you create a UIImage from your image file, then create a UIImageView from that: let imageName = "yourImage. png" let image = UIImage(named: imageName) let imageView = UIImageView(image: image!)


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

Hope this helps . . .

like image 193
Raj Pawan Gumdal Avatar answered Sep 22 '22 08:09

Raj Pawan Gumdal