How to load an image in prepareForInterfaceBuilder with a IBDesignable UIImageView(如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像)
问题描述
我想在 IB 可设计的 UIImageView
中加载示例图像,以便在编辑界面时显示在 Interface Builder 中.以下代码不起作用,因为 IB 中的视图占位符仍然为空(视图区域仅包含 UIImageView 文本):
I would like to load a sample image in an IB designable UIImageView
, to be shown in Interface Builder while editing the interface. The following code does not work, as the view placeholder in IB remains empty (the view area contains only the UIImageView text):
@IBDesignable
class TestImageView : UIImageView
{
override func prepareForInterfaceBuilder() {
//let bundle = NSBundle.mainBundle()
let bundle = NSBundle(forClass: nil)
let imagePath = bundle.pathForResource("Test", ofType: "jpg")
self.image = UIImage(contentsOfFile: imagePath)
}
}
注意:
- 在 IB 中,视图的自定义类是正确的 (TestImageView)
- Test.jpg 存在于项目中(如果我在 IB 中手动设置
UIImageView
的图像属性,图像就会显示出来). - 我尝试了两种不同的方法来获取代码中存在的包
- in IB the Custom Class class for the view is correct (TestImageView)
- Test.jpg is present in the project (if I manually set the image property of the
UIImageView
in IB the image shows up). - I tried the two different methods of getting the bundle present in the code
这是使用 Xcode 6 beta 3 测试的.
This was tested with Xcode 6 beta 3.
更新:在这两种情况下,我得到的包路径都是 "/Applications/Temporary/Xcode6-Beta3.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/叠加层"
.在该路径中,图像显然不存在.
Update: in both cases the bundle path I get is "/Applications/Temporary/Xcode6-Beta3.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays"
. In that path the image is obviously not present.
推荐答案
为 Swift 4.2 更新
当您实例化 UIImage(使用 UIImage(named : "SomeName"))时,应用程序将在您的 主包 中查找资产,这通常可以正常工作.但是当您在 设计时,InterfaceBuilder 将可设计视图的代码(用于编译同时设计)在一个单独的包中.
Updated for Swift 4.2
When you instantiate an UIImage (with UIImage(named : "SomeName") the app will look for the asset in your main bundle, which works fine usually. But when you are at design time, the InterfaceBuilder holds the code of the designable views (for compiling while designing) in a separate bundle.
因此解决方案是:动态定义您的捆绑包,因此您的文件可以在设计、编译和运行时找到:
So the solution is: Define your bundle dynamically, hence your files can be found in design, compile and run time:
// DYNAMIC BUNDLE DEFINITION FOR DESIGNABLE CLASS
let dynamicBundle = Bundle(for: type(of: self))
// OR ALTERNATIVELY BY PROVDING THE CONCRETE NAME OF YOUR DESIGNABLE VIEW CLASS
let dynamicBundle = Bundle(for: YourDesignableView.self)
// AND THEN SUCCESSFULLY YOU CAN LOAD THE RESSOURCE
let image = UIImage(named: "Logo", in: dynamicBundle, compatibleWith: nil)
这篇关于如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像


- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01