Failed to render instance of IB Designables

2019-01-12 23:08发布

I have an Objective-C and Swift mixed dynamic framework. And the mixed framework was linked with two pure Objective-C dynamic frameworks.

When I tried to mark any class in the mixed framework with IB Designable and using that class in either storyboard or nib, the Xcode always says the instance of it was failed to render.

And there was the error message:

IB Designables: Failed to render instance of WZUITokenField: dlopen(WZUIKit.framework, 1): Library not loaded: /Library/Frameworks/WZFoundation.framework/WZFoundation Referenced from: WZUIKit.framework Reason: image not found

IB Designables: Failed to update auto layout status: dlopen(WZUIKit.framework, 1): Library not loaded: @rpath/WZFoundation.framework/WZFoundation Referenced from: WZUIKit.framework Reason: image not found

The framework WZUIKit is an Objective-C and Swift mixed framework and the WZFoundation is pure Objective-C.

Plus, all these sutff work on either device or the simulator.

13条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-12 23:44

In my case, I was doing the next in the initWithFrame/initWithCoder methods to create the view:

className = NSStringFromClass([self class]);
self.view = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject];

But It looks like I was not supposed to use the Main Bundle but the bundle of the class. So I replaced that code for the following and it worked:

bundle = [NSBundle bundleForClass:[self class]];
className = NSStringFromClass([self class]);
self.view = [[bundle loadNibNamed:className owner:self options:nil] firstObject];

I thought maybe this might help somebody.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-12 23:46

I hit this error on a framework containing IBDesignables.

I had to add $(FRAMEWORK_SEARCH_PATHS) to the LD_RUNPATH_SEARCH_PATHS setting on my framework target.

After updating the runpath setting, building, and refreshing the view in the storyboard, the error went away. Didn't even have to clean the build folder.

enter image description here

查看更多
闹够了就滚
4楼-- · 2019-01-12 23:51

I had same problem In Xcode 6.4, adding Test target membership to the storyboard fixed these errors for me.

Also my test target has @loader_path/Frameworks set in Runpath Search Paths for test target, and not in original target.

enter image description here

查看更多
可以哭但决不认输i
5楼-- · 2019-01-12 23:53

Finally, I solved this issue by adding $(CONFIGURATION_BUILD_DIR) in the target's build settings' Runpath Search Paths field.

Plus, there are some additional steps you might need to do with your Xcode.

  1. Clear Xcode derived data for the project. They are in ~/Library/Developer/Xcode/DerivedData
  2. Clean your current build by pressing K
  3. Build your project
  4. In storyboard go to Editor menu and do Refresh All Views; wait for build to be completed and errors should be gone

Credit to @Mojtaba

查看更多
戒情不戒烟
6楼-- · 2019-01-13 00:01

I had a similar error which was caused by my Framework's views not having public initialisers:

public override init(frame: CGRect) {
    super.init(frame:frame)
    commonInit()
}

required public init(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}
查看更多
看我几分像从前
7楼-- · 2019-01-13 00:03

I just figured out another reason in my case:

When I used 'Editor -> Debug Selected Views' I saw that it crashed, because I was using CoreGraphics to create an image and use it as background image for a button:

class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {

    let rect = CGRectMake(0.0, 0.0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
    let context = UIGraphicsGetCurrentContext();    // It's alraedy nil here :(

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);

    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

The reason is simply that size is (0.0, 0.0). Why is that? I am calling

super.setBackgroundImage(UIImage.imageWithColor(bgColor, size: self.bounds.size), forState: .Normal)

But in IB self.bounds.size is still 0 at this point! So I changed one line:

let rect = CGRectMake(0.0, 0.0, max(size.width, 1.0), max(size.height, 1.0));

And now it works :)

Apple should provide a list with Dos and Don'ts regarding the IB...

查看更多
登录 后发表回答