Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using named colors from asset catalogue in IBDesignable Views crashes IB even when specifying the Bundle

When referencing a colour that has been created using UIColor(named:) Interface Builder crashes due to it finishing nil for the colour.

Initially I thought this would be as IB was looking for the colour in the wrong bundle but after specifying the Bundle to look in IB could still not find the colour.

This is working fine at runtime on device/simulator.

extension UIColor {

    // MARK: - Oranges
    static let tangerine = UIColor(named: "Tangerine", in: Bundle(for: AppDelegate.self), compatibleWith: nil)!
    .....
}

Looking into it a bit further the Bundle which IB looks in does seem to contain references to the colours as well as images so you would expect it to work..?

po Bundle(for: AppDelegate.self)
NSBundle </Users/me/Library/Developer/Xcode/DerivedData/MyApp-ddspmruccrhrxhbawgwawhzkrrts/Build/Intermediates.noindex/IBDesignables/Products/Debug - DEV-iphonesimulator/MyApp.app> (loaded)`

xcrun --sdk iphoneos assetutil --info pathToAsserts.car

  {
"AssetType" : "Color",
"Color components" : [
  1,
  1,
  1,
  1
],
"Colorspace" : "srgb",
"Idiom" : "universal",
"Name" : "Marmalade",
"Scale" : 1
},
{
"AssetType" : "Image",
"BitsPerComponent" : 8,
"ColorModel" : "RGB",
"Colorspace" : "srgb",
"Compression" : "lzfse",
"Encoding" : "ARGB",
"Idiom" : "universal",
"Image Type" : "kCoreThemeOnePartScale",
"Name" : "sweets",
"Opaque" : false,
"PixelHeight" : 412,
"PixelWidth" : 324,
"RenditionName" : "[email protected]",
"Scale" : 2,
"SizeOnDisk" : 132830
},

Edit: Turns out specifying the bundle as above is enough. Cleaning the build folder, deleting derived data and restarting Xcode made it all work as expected

like image 357
bencallis Avatar asked Sep 10 '18 13:09

bencallis


2 Answers

One thing I noticed fighting with this: If your fonts are in the main bundle, using Bundle.main doesn't work but using Bundle(for: AppDelegate.self) (or some other class in your main bundle) does.

Probably something to do with the way IBDesignablesAgent loads everything into its process, but I definitely did not expect that.

like image 92
DesignatedNerd Avatar answered Oct 21 '22 21:10

DesignatedNerd


Turns out it was just Xcode playing up. Cleaning the build folder, derived data and restarting Xcode fixed the issue (when specifying the correct bundle as shown in my code snippet).

like image 1
bencallis Avatar answered Oct 21 '22 22:10

bencallis