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
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With