简体   繁体   English

使用IBDesignable Views中资产目录中的命名颜色即使在指定Bundle时也会崩溃IB

[英]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.

相关问题 模拟内存警告时,IB的GestureRecognizers使应用程序崩溃 - GestureRecognizers from IB crashes app when simulating memory warning 向数据源添加值时,带有CollectionView的IBDesignable崩溃 - IBDesignable with CollectionView crashes when add value to DataSource 引用资产目录并从框架中包含资产目录都会创建重复项吗? - Does Both Referencing an Asset Catalogue and Including it from a Framework Create Duplication? 从 Testflight 加载应用程序时捆绑资产检索中断 - Bundle asset retrieval broken when app is loaded from Testflight 没有IBInspectables的IBDesignable UIButton IB渲染 - IBDesignable UIButton IB Render without IBInspectables 为什么我的 colors 目录中的动态 colors 在我的导航栏上不起作用? - Why do dynamic colors from my colors catalogue do not work on my navigation bar? 从UserDefaults(Swift)加载颜色时,应用程序启动时崩溃 - App crashes on start when load colors from UserDefaults (Swift) 使用命名颜色时,Xcode 9.2停止编译&lt;11.0目标 - Xcode 9.2 stops compiling for < 11.0 target when using named colors 自定义@IBDesignable UIButton 崩溃 Xcode - Custom @IBDesignable UIButton crashes Xcode 如何在启动故事板中使用资产目录中的启动映像? - How do I use the Launch Images from an Asset Catalogue in the Launch Storyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM