Static library, bundle and IB Designable

1.8k views Asked by At

I'm trying to use the IB Designable and IB Inspectable in my UIButton but seems there are some errors.

In the Issue Navigator, the bundle file:

Storyboard : IB Designables : Failed to update auto layout status: Failed to load designabled from path (null)

So I wonder if there are any configurations to do to use it in a static lib or something like that ?

This is

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface CustomUIButton : UIButton

@property (nonatomic) IBInspectable int cornerRadius;

@end

My custom button I want to live render is inside a view controller

2

There are 2 answers

2
Keith Smiley On

Unfortunately it isn't possible to use IBDesignables with static libraries (or static frameworks).

This isn't a great answer but I want to give some context on why.

It seems that the way Interface Builder loads classes to be shown as designables is by actually dynamically loading the dynamic framework that you create (and not your app's binary), and using the classes directly (after changing them using the Objective-C runtime quite a bit).

You can see that Interface Builder isn't loading your app, and just the individual frameworks with these steps:

  1. Create a new Xcode project
  2. Create a new framework target in the project
  3. Add a class that is IBDesignable
  4. In the storyboard from your app create a view and set its class to your framework's IBDesignable class
  5. Click "Refresh All Views" in the "Editor" menu
  6. In your DerivedData folder for the project, in the IBDesignables directory, you can see that only your framework target has been built.

Interface builder actually loads your framework using dlopen manually. You can also see that to facilitate this when building your framework, Interface Builder actually adds 2 RPATHs to your binary, so the dependencies can be found in the custom paths (you can view this with otool -L frameworkbinary). This method of loading your binary isn't possible with static libraries.

For what it's worth I think the best workaround for this is to build dynamic frameworks, instead of static libraries, but only for IBDesignable builds. You'll have to do some configuration work to do this, and it isn't easy to work around Xcode to have this work, but if you try it, you can use either the build path, or the environment, to differentiate IBDesignable builds vs "normal" builds.

1
Edgar On

EDIT: IB Designable and IB Inspectable do not seem to work with static libraries, so if you're using a static library, either consider using a dynamic framework (iOS 8 only) or move your component outside the static library, if possible.

For issues related with Cocoapods, use the following solution:

This is related to this question.

Here's the solution:

1) Install Cocoapods (0.36.0.beta.1) or newer;

2) Add use_frameworks! to your Podfile

See more here.