xcode 6 IB_DESIGNABLE- not loading resources from bundle in Interface builder

前端 未结 6 1132
难免孤独
难免孤独 2020-12-02 17:12

I am trying to make a custom control that updates live in Interface Builder using the new IB_DESIGNABLE option described here.

- (void)drawRect:(CGRect)rect
         


        
相关标签:
6条回答
  • 2020-12-02 17:45

    I've fixed it using the following line of code:

    let image = UIImage(named: "image_name", inBundle: NSBundle(forClass: self.dynamicType), compatibleWithTraitCollection: nil)
    

    After implementing it this way the images show up correctly in the Interface Builder

    0 讨论(0)
  • 2020-12-02 17:48

    I ran into a similar problem in Swift. As of Xcode 6 beta 3, you don't need to use a framework to get live rendering. However, you still have to deal with the bundle problem for Live View so that Xcode knows where to find the assets. Assuming "btn_slider_off" is an image set in Images.xcassets then you can do this in Swift for live rendering and it will work when the app is run normally as well.

    let name = "btn_slider_off"
    let myBundle = NSBundle(forClass: self.dynamicType)
    // if you want to specify the class name you can do that instead
    // assuming the class is named CustomView the code would be
    // let myBundle = NSBundle(forClass: CustomView.self)
    let image = UIImage(named: name, inBundle: myBundle, compatibleWithTraitCollection: self.traitCollection)
    if let image = image {
        image.drawInRect(self.bounds)
    }
    
    0 讨论(0)
  • 2020-12-02 17:51

    I also had the same problem : looking at this answer and following WWDC 2014 whats new in Interface Builder. I solved it like that:

    - (void)prepareForInterfaceBuilder{
         NSArray *array =    [[NSProcessInfo processInfo].environment[@"IB_PROJECT_SOURCE_DIRECTORIES"] componentsSeparatedByString:@":"];
         if (array.count > 0) {
             NSString *str = array[0];
             NSString *newStr =  [str stringByAppendingPathComponent:@"/MyImage.jpg"];
             image = [UIImage imageWithContentsOfFile:newStr]; 
         }
    }
    
    0 讨论(0)
  • 2020-12-02 17:57

    As of when this question was first asked, creating an IB-designable control required packaging it in a framework target. You don't have to do that anymore — the shipping Xcode 6.0 (and later) will preview IB-designable controls from your app target, too. However, the problem and the solution are the same.

    Why? [NSBundle mainBundle] returns the primary bundle of the currently running app. When you call that from a framework, you're getting a different bundle returned based on which app is loading your framework. When you run your app, your app loads the framework. When you use the control in IB, a special Xcode helper app loads the framework. Even if your IB-designable control is in your app target, Xcode is creating a special helper app to run the control inside of IB.

    The solution? Call +[NSBundle bundleForClass:] instead (or NSBundle(forClass:) in Swift). This gets you the bundle containing the executable code for whichever class you specify. (You can use [self class]/self.dynamicType there, but beware the result will change for subclasses defined in different bundles.)

    If you're using the framework approach — which can be useful for some apps even though it's no longer required for IB-designable controls — it's best to put image resources in the same framework with the code that uses them. If your framework code expects to use resources provided at run time by whatever app loads the framework, the best thing to do for making it IB-designable is to fake it. Implement the prepareForInterfaceBuilder method in your control and have it load resources from a known place (like the framework bundle or a static path in your Xcode workspace).

    0 讨论(0)
  • 2020-12-02 18:04

    Warning about the above bundle/path resolution while in IB_DESIGNABLE:

    XCode will not resolve a resource if the call contained in the initialization of the UIView. I could only get XCode to resolve a path while in drawRect:

    I found a great / easy workaround to the above bundle resolution techniques, just simply designate a IBInspectable property to be a UIImage, then specify the image in Interface Builder. Ex:

    @IBInspectable var mainImage: UIImage?
    @IBInspectable var sideImage: UIImage?
    
    0 讨论(0)
  • 2020-12-02 18:06

    IN SWIFT 3.0 the code of @rickster has changed to:

    // DYNAMIC BUNDLE DEFINITION FOR DESIGNABLE CLASS
    let theBundle : Bundle = Bundle(for: type(of: self))
    
    // OR ALTERNATEVLY BY PROVDING THE CONCRETE NAME OF DESIGNABLE CLASS
    let theBundle : Bundle = Bundle(for: RH_DesignableView.self)
    
    // AND THEN SUCCESSFULLY YOU CAN LOAD THE RESSOURCE
    let theImage  : UIImage? = UIImage(named: "Logo", in: theBundle, compatibleWith: nil)
    
    0 讨论(0)
提交回复
热议问题