Apple Interface Builder:将子视图添加到 UIImageView [英] Apple Interface Builder: adding subview to UIImageView

查看:23
本文介绍了Apple Interface Builder:将子视图添加到 UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Interface Bulder 的帮助下创建了 UIImageView.现在我想在其中放置标签(作为它的子视图).在代码中我可以输入如下内容: [myUIImageView addSubview:myUILabel]; 但是我可以在 IB 的帮助下完成吗?我找到了 UIView 的解决方案,但找不到 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.

推荐答案

您不能在界面生成器中向 UIImageView 添加子视图,原因只有 Apple 知道!您说得对,您可以通过编程方式添加子视图,但是设置自动调整大小的掩码和子视图的位置的开销都应该在代码中处理,这很麻烦.

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.

所以有一个简单的解决方法.无需在笔尖中拖动 UIImageView 的实例,只需拖动 UIView 并将其类从 UIView 更改为 UIImageView(检查器的 cmd+4 选项).您在默认 imageView 实例和新 UIImageView 子类实例的 nib 中发现的唯一区别是:您不能从 nib(cmd+1 选项)将图像设置为新的 imageView.所以,在其对应的viewController的-viewDidLoad方法中,将image设置到UIImageView的这个outlet.

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.

通过这样做,您可以在界面构建器中自由地将子视图添加到现在的 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 . . .

这篇关于Apple Interface Builder:将子视图添加到 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆