[英]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. 当引用使用
UIColor(named:)
创建的颜色时UIColor(named:)
Interface Builder崩溃,因为它的颜色为nil。
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. 最初我认为这将是因为IB正在寻找错误束中的颜色但是在指定Bundle以查看IB之后仍然无法找到颜色。
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..? IB看起来的Bundle看起来确实包含对颜色和图像的引用,所以你会期望它能够工作......?
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" : "sweets@2x.png",
"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
清理构建文件夹,删除派生数据并重新启动Xcode使其全部按预期工作
Turns out it was just Xcode playing up. 原来只是Xcode正在播放。 Cleaning the build folder, derived data and restarting Xcode fixed the issue (when specifying the correct bundle as shown in my code snippet).
清理构建文件夹,派生数据并重新启动Xcode修复了问题(当指定正确的包时,如我的代码片段所示)。
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. 有一两件事我注意到这个战斗:如果您的字体是主束,用
Bundle.main
不工作,但使用Bundle(for: AppDelegate.self)
或在您的主束一些其他类)一样。
Probably something to do with the way IBDesignablesAgent
loads everything into its process, but I definitely did not expect that. 可能与
IBDesignablesAgent
将所有内容加载到其进程中的方式IBDesignablesAgent
,但我绝对没想到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.