我希望在IB可編程的UIImageView
中加載示例圖像,以便在編輯界面時在Interface Builder中顯示。下面的代碼無法正常工作,在IB視圖佔位符保持爲空(視圖區域僅包含UIImageView的文字):如何使用IBDesignable在prepareForInterfaceBuilder中加載圖像UIImageView
@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
的圖像屬性,圖像顯示出來)。 - 我試圖讓本捆綁在代碼
的這是在Xcode 6公測3
更新測試了兩種不同的方法:在這兩種情況下的束路徑我得到的是"/Applications/Temporary/Xcode6-Beta3.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays"
。在那條路上,圖像顯然不存在。
張貼雷達到蘋果rdar:// 17762946 – mxb
說,你不應該在prepareForInterfaceBuilder中調用超級? – Fattie