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
.
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.
An object that manages image data in your app.
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!)
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 . . .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With