ios IB_DESIGNABLE, IBInspectable -- 界面构建器不更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26674111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IB_DESIGNABLE, IBInspectable -- Interface builder does not update
提问by nhgrif
I have the following set of code:
我有以下一组代码:
CustomView.h
自定义视图.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomView : UIView
@property (nonatomic) IBInspectable UIColor *borderColor;
@property (nonatomic) IBInspectable CGFloat borderWidth;
@property (nonatomic) IBInspectable CGFloat cornerRadius;
@end
CustomView.m
自定义视图.m
#import "CustomView.h"
@implementation CustomView
- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
self.layer.borderColor = borderColor.CGColor;
}
- (void)setBorderWidth:(CGFloat)borderWidth {
_borderWidth = borderWidth;
self.layer.borderWidth = borderWidth;
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
}
@end
(For Swift reference, this problem was also occurring with Swift code)
(对于 Swift 参考,这个问题也出现在 Swift 代码中)
CustomView.swift
自定义视图.swift
@IBDesignable
class CustomView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBInspectable var borderColor : UIColor = UIColor.clearColor() {
didSet {
self.layer.borderColor = borderColor.CGColor
}
}
@IBInspectable var borderWidth : CGFloat = 0.0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var cornerRadius : CGFloat = 0.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
}
I added a UIView
to a view controller on the storyboard and set its subclass to CustomView
.
我将 a 添加UIView
到故事板上的视图控制器并将其子类设置为CustomView
.
This adds the "Designables" row. It is stuck on "Updating" and the tooltip says "Waiting for Target to Build". It never changes from this status.
这将添加“Designables”行。它停留在“正在更新”上,工具提示显示“正在等待目标构建”。它永远不会从这个状态改变。
When I move to the attributes inspect, I am able to set these IBInspectable
properties:
当我转到属性检查时,我可以设置这些IBInspectable
属性:
And once set, they also show up in the "User Defined Runtime Attributes":
一旦设置,它们也会显示在“用户定义的运行时属性”中:
However, the "Designables" status never moves beyond "Updating" with still the same tooltip (I've tried Cmd+B building several times, nothing changes).
然而,“Designables”状态永远不会超过“更新”,工具提示仍然相同(我已经多次尝试 Cmd+B 构建,没有任何变化)。
Moreover, as I set the IBInspectable
properties, I get a warning for each one:
此外,当我设置IBInspectable
属性时,我会收到每个属性的警告:
IBDesignables- Ignoring user defined runtime attribute for key path "borderColor" on instance of "UIView" ... this class is not key-value coding-compliant for the key borderColor.
IBDesignables- 忽略“UIView”实例上的关键路径“borderColor”的用户定义的运行时属性......该类不符合关键borderColor的键值编码。
Screenshot of the warnings generated:
生成的警告截图:
I am familiar with the key-value coding-compliant issues and generally know how to solve them... but I don't understand how to solve this issue here. According to the view's identity inspector, the view is a "CustomView" (not a regular "UIView", which doesn't have these properties). And if the view weren't a "CustomView" then these designable properties wouldn't show up in the Attributes Inspector, right? But when Interface Builder tries to apply these attributes to the view, it goes back to thinking the view's class is "UIView" and cannot apply the attributes.
我熟悉键值编码兼容问题,并且通常知道如何解决它们......但我不明白如何在这里解决这个问题。根据视图的身份检查员,视图是“CustomView”(不是常规的“UIView”,它没有这些属性)。如果视图不是“CustomView”,那么这些可设计的属性就不会显示在 Attributes Inspector 中,对吧?但是当 Interface Builder 尝试将这些属性应用于视图时,它又回到认为视图的类是“UIView”并且无法应用这些属性。
Any help? Please let me know if I've left out some important detail, but for what it's worth, I followed this tutorial exactly(other than ObjC vs Swift). It's also worth noting that I followed this tutorial exactly on another machine and it worked like a charm (I intended to make this post last night but the computer I was on then didn't have this issue).
有什么帮助吗?如果我遗漏了一些重要的细节,请告诉我,但就其价值而言,我完全遵循了本教程(ObjC 与 Swift 除外)。还值得注意的是,我完全在另一台机器上遵循了本教程,它的效果非常好(我昨晚打算发布这篇文章,但当时我使用的计算机没有这个问题)。
Based on comments, it has been suggest that perhaps the .m
file isn't included and that might be causing the problem. I thought surely I would have gone out of my way for this scenario to be the case, but I checked anyway.
根据评论,有人建议该.m
文件可能未包含在内,这可能是导致问题的原因。我想我肯定会为这种情况而竭尽全力,但我还是检查了。
When I first started attempting to do this, I was under the understanding that the IB_DESIGNABLE
classes had to be part of a different UIKit
framework. So from this first screenshot, you can see that I set up a "CustomViews" framework, which has one class, CustomView
. You'll also see here that I also created a OtherView
, which is identical to CustomView
, except it's not in a separate framework. The identical problem persists on the storyboard between both classes however.
当我第一次开始尝试这样做时,我了解到这些IB_DESIGNABLE
类必须是不同UIKit
框架的一部分。所以从第一个截图中,你可以看到我设置了一个“CustomViews”框架,它有一个类,CustomView
. 您还将在此处看到我还创建了一个OtherView
,它与 相同CustomView
,只是它不在单独的框架中。然而,两个类之间的故事板上仍然存在相同的问题。
Here we have a screenshot indicating that CustomView.m
is included to be built with the CustomViews
framework:
这里我们有一个屏幕截图,表明它CustomView.m
包含在CustomViews
框架中构建:
Meanwhile, the following screenshot indicates several things:
同时,以下屏幕截图表明了几件事:
CustomViews.framework
is appropriately included in the main project.OtherView.m
is also included as a compile source, so even if something is wrong withCustomView
,OtherView
should work, however it generates identical errors.Main.storyboard
andLaunchScreen.xib
are showing up as red. I have no idea why, and haven't the slightest clue as to whyLaunchScreen.xib
should (I haven't touched this file), though I can say after looking at other projects,Main.storyboard
also shows up in red for those projects, and I'm not doing anything withIB_DESIGNABLE
orIBInspectable
there.
CustomViews.framework
适当地包含在主要项目中。OtherView.m
也作为编译源包含在内,因此即使 出现问题CustomView
,也OtherView
应该可以工作,但是它会产生相同的错误。Main.storyboard
并且LaunchScreen.xib
显示为红色。我不知道为什么,也不知道为什么LaunchScreen.xib
应该(我没有接触过这个文件),虽然我可以在查看其他项目后说,这些项目Main.storyboard
也显示为红色,我不做任何事情IB_DESIGNABLE
或IBInspectable
那里。
I have tried and retried this several times now. It works every time on my computer at home--I can not reproduce the problem described in this question at home. At work, it never works. The problem described in this question happens every time.
我已经尝试并重试了几次。它每次都在我家里的电脑上运行——我在家无法重现这个问题中描述的问题。在工作中,它永远不会奏效。这个问题描述的问题每次都会发生。
Both computers are Mac Minis purchased new this year (not the new models, late 2012 model). Both computers are running OS X Yosemite 10.10. Both computers are running Xcode Version 6.1. At home, the build is (6A1052d). This morning, I can confirm that both computers are running identical builds of Xcode.
这两台电脑都是今年新购买的 Mac Mini(不是新型号,2012 年末型号)。两台计算机都运行 OS X Yosemite 10.10。两台计算机都运行 Xcode 6.1 版。在家里,版本是 (6A1052d)。今天早上,我可以确认两台计算机都运行相同版本的 Xcode。
Others have suggested to me that it might be bad RAM. That seems far fetched to me. I've restarted the project multiple times, restarted the computer multiple times. Seems to me if there were bad RAM on a computer approximately 6 months old, that I'd be seeing other problems, and that this problem would be less consistent. But this exact problem persists despite numerous times restarting the entire project from scratch and full restarts on the computer.
其他人向我建议它可能是坏 RAM。这对我来说似乎有些牵强。我已经多次重新启动项目,多次重新启动计算机。在我看来,如果大约 6 个月大的计算机上的 RAM 不好,我就会看到其他问题,并且这个问题会不太一致。但是,尽管无数次从头开始重新启动整个项目并在计算机上完全重新启动,但这个确切的问题仍然存在。
It should be worth noting that if I actually compile and run this project, the custom view with the IBInspectable
properties actually displays as I expect the storyboard to display it. I imagine that this would be the case even with out the IB_DESIGNABLE
and IBInspectable
directives however, as these are created as User Defined Runtime Attributes.
值得注意的是,如果我实际编译并运行这个项目,带有IBInspectable
属性的自定义视图实际上会按照我期望的故事板显示它的方式显示。我想即使没有IB_DESIGNABLE
andIBInspectable
指令也会是这种情况,因为它们是作为用户定义的运行时属性创建的。
采纳答案by nhgrif
Based on chrisco's suggestion to debug the selected view (which I had already done, but went to try again for good measure), I noticed a couple of other options at the bottom of the Editor menu.
根据 chrisco 对调试所选视图的建议(我已经完成了,但为了更好的衡量而再试一次),我注意到编辑器菜单底部有几个其他选项。
- Automatically Refresh Views
- Refresh All Views
- 自动刷新视图
- 刷新所有视图
I clicked "Refresh All Views" and after Xcode had a bit of a think, suddenly the storyboard was displaying my view as expected (properly applying my IBInspectable
properties).
我点击了“刷新所有视图”,在 Xcode 想了想之后,突然故事板按预期显示了我的视图(正确应用了我的IBInspectable
属性)。
I then went through the whole process again to confirm that this is the solution.
然后我再次经历了整个过程以确认这是解决方案。
I created a new class, ThirdView
. This class is identical to the others, again. I changed my view's class to ThirdView
and got something slightly different this time:
我创建了一个新类,ThirdView
. 这个类与其他类相同。ThirdView
这次我将视图的类更改为并略有不同:
Clicking "Show" to me to the warnings:
点击“显示”给我看警告:
A new one this time:
这次是新的:
Using class UIView for object with custom class because the class ThirdView does not exist.
将类 UIView 用于具有自定义类的对象,因为类 ThirdView 不存在。
This isn't really any more helpful than what already existed. Plus, now the other three warnings have doubled into 6 strangely.
这实际上并不比已经存在的更有帮助。另外,现在其他三个警告奇怪地翻了一番,变成了 6 个。
Anyway, if I click "Refresh All Views" from the Editor drop down menu again, all the errors go away, and once again, the view properly displays.
无论如何,如果我再次从编辑器下拉菜单中单击“刷新所有视图”,所有错误都会消失,并且视图再次正确显示。
Still, up to this point, everything I did was stuff I never messed with at home. At home, it just worked. So I turned on "Automatically Refresh Views" and created a "FourthView" to test--once again, identical to the first three.
尽管如此,到目前为止,我所做的一切都是我在家里从未做过的事情。在家里,它只是有效。所以我打开了“自动刷新视图”并创建了一个“FourthView”来测试——再次,与前三个相同。
After changing the view's class to "FourthView" the designables label said "Updating" for a short moment then finally said "Up to date":
将视图的类更改为“FourthView”后,designables 标签短暂地显示“更新”,然后最后说“最新”:
So, I checked my computer at home. "Automatically Refresh Views" is turned on at the computer that was always working. It was turned off at the computer that wasn't. I don't ever remember touching this menu option. I can't even tell you for sure whether it existed before Xcode 6. But this option is what was making the difference.
所以,我在家里检查了我的电脑。在一直工作的计算机上打开了“自动刷新视图”。它在不是的计算机上被关闭。我不记得触摸过这个菜单选项。我什至不能确定它在 Xcode 6 之前是否存在。但这个选项是造成差异的原因。
TL;DR, if you're having the same problem described in the question, make sure "Automatically Refresh Views" is turned on (or manually "Refresh All Views" when you need an update in IB):
TL; DR,如果您遇到问题中描述的相同问题,请确保“自动刷新视图”已打开(或在 IB 中需要更新时手动“刷新所有视图”):
回答by Martin-Gilles Lavoie
I have a few more details that may cause your IBDesignable classes to not be loaded.
我还有一些细节可能会导致您的 IBDesignable 类无法加载。
Select your problematic storyboard/xib where your custom views ought to display.
选择您的自定义视图应该显示的有问题的故事板/xib。
In the navigator area, head to the Report Navigator in your XCode workspace/project.
在导航器区域,前往 XCode 工作区/项目中的 Report Navigator。
In the Editor menu of XCode, hit (as mentioned by nhgrif), the "Refresh All Views" option. This will cause IB to launch a compile for a whole bunch of stuff that you, I'm certain, would not expect.
在 XCode 的编辑器菜单中,点击(如 nhgrif 所述),“刷新所有视图”选项。这将导致 IB 为一大堆东西启动编译,我敢肯定,你不会期望的。
In the Report Navigator, Click on "By Group" to filter content and look at the "Interface Builder" section. You will see that for the sake of loading the custom IBDesignable views framework, it will compile LOTS of things. If anyof these targets do NOT compile, such as (perhaps deprecated) unit test targets (even if they are totally unrelated to the code that loads these views or storyboard), then IB will fail at loading your dll.
在 Report Navigator 中,单击“By Group”以过滤内容并查看“Interface Builder”部分。你会看到为了加载自定义的 IBDesignable 视图框架,它会编译很多东西。如果这些目标中的任何一个未编译,例如(可能已弃用)单元测试目标(即使它们与加载这些视图或故事板的代码完全无关),那么 IB 将无法加载您的 dll。
In my case, IB tried to compile 8 targets, including 4 that where unit tests that had not been updated since recent refactoring changes we've been working on.
就我而言,IB 尝试编译 8 个目标,其中 4 个目标是自我们最近进行的重构更改以来尚未更新的单元测试。
Most of the code changes/fixes I have done in order for IB to properly load and display my customs views where not related or even linked against these classes, nor would it ever load the storyboard in the course of running these unit tests. Yet, IB had a dependency on the whole workspace compiling for it to work.
我所做的大多数代码更改/修复都是为了让 IB 正确加载和显示我的自定义视图,而这些视图与这些类无关,甚至与这些类无关,也不会在运行这些单元测试的过程中加载故事板。然而,IB 依赖于整个工作区编译以使其工作。
回答by GoldenJoe
Just a quick hint for anyone else having this problem: remember to specify the type of the variable.
给遇到此问题的其他人一个快速提示:记住指定变量的类型。
// Doesn't show up in IB
@IBInspectable var includeLeftSection = true
// Shows now that it knows the type
@IBInspectable var includeLeftSection : Bool = true
回答by samwize
I had the same warning Ignoring user defined runtime attribute for key path ..
even though I am absolutely sure I didn't do anything wrong with my custom IBDesignable view class.
Ignoring user defined runtime attribute for key path ..
即使我绝对确定我的自定义 IBDesignable 视图类没有做错任何事情,我也收到了同样的警告。
Turned out, in my case, it got to do with Xcode cache.
原来,就我而言,它与 Xcode 缓存有关。
rm -rf ~/Library/Developer/Xcode/DerivedData/*
Purge DerivedData
and the warning is gone.
清除DerivedData
,警告消失。
回答by Dave Thomas
Incase any one else comes up against the error IB Designables class does not exist, for the same reason as I did. Top answer was not my issue... but here is a slightly related problem...
以防万一其他人遇到错误 IB Designables class does not exist,原因与我相同。最佳答案不是我的问题......但这里有一个稍微相关的问题......
There is a property hidden in the story board source code called customModule.
故事板源代码中隐藏了一个名为 customModule 的属性。
For example I had a class called ForwardArrow inside a separate framework that I accidentally added to my main target.
例如,我在一个单独的框架中有一个名为 ForwardArrow 的类,我不小心将它添加到了我的主要目标中。
So the XML for some views ended up as customClass="ForwardArrow" customModule="MainTargetNameWasHere"
所以一些视图的 XML 最终变成了 customClass="ForwardArrow" customModule="MainTargetNameWasHere"
When I removed them from the main target in the build the story board did not update MainTargetNameWasHere to CustomViews which is the framework where it was located and started giving that no class found error.
当我从构建中的主要目标中删除它们时,故事板没有将 MainTargetNameWasHere 更新为 CustomViews,这是它所在的框架,并开始给出没有发现类的错误。
So TLDR; Make sure that if your IBDesignable is in another framework that the customModule xml attribute in your story board is set to the right value. And if it isn't there at all add it.
所以 TLDR; 确保如果您的 IBDesignable 在另一个框架中,则故事板中的 customModule xml 属性设置为正确的值。如果它根本不存在,请添加它。
Example from my source:
来自我的来源的例子:
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="MUG-jc-2Ml" customClass="ForwardArrow" customModule="CustomViews">
回答by Even Cheng
As my example, I was using CheckboxButtonvia pod and the graphics of checkbox never shows up in the storyboard while I got the same issues described in the question here:
作为我的示例,我通过 pod使用CheckboxButton并且复选框的图形从未出现在故事板中,而我在这里遇到了问题中描述的相同问题:
warning: IB Designables: Using class UIView for object with custom class because the class CheckboxButton does not exist
警告:IB Designables:将类 UIView 用于具有自定义类的对象,因为类 CheckboxButton 不存在
and
和
warning: IB Designables: Ignoring user defined runtime attribute for key path "checkColor" on instance of "UIView". Hit an exception when attempting to set its value: [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key checkColor.
警告:IB Designables:忽略“UIView”实例上关键路径“checkColor”的用户定义运行时属性。尝试设置其值时遇到异常:[ setValue:forUndefinedKey:]: 此类不是键 checkColor 的键值编码兼容。
The way solved my problem was to supply the module with name CheckboxButtonas below:
解决我的问题的方法是提供名为CheckboxButton的模块,如下所示:
Note: you should replace CheckboxButtonto whatever the name of module you are using.
注意:您应该将CheckboxButton替换为您正在使用的任何模块名称。
回答by HannahCarney
I personally solved this problem by using the "-" button to delete content from my identity inspector. When you remove custom classes, change content in the IB and then add a new custom class, the designable elements in the identity inspector don't get removed and it caused me to have that error. Just Delete everything and rebuild.
我个人通过使用“-”按钮从我的身份检查器中删除内容来解决这个问题。当您删除自定义类、更改 IB 中的内容然后添加新的自定义类时,身份检查器中的可设计元素不会被删除,这导致我出现该错误。只需删除所有内容并重建即可。
回答by Gordon Dove
I know this is answered, but here is one more experience.
我知道这已经得到了回答,但这里还有一个经验。
I was having some problems unrelated to this issue, but in the process I removed @IBInspectable from the vars in my class and deleted the attributes from the identity inspector (alt-apple-3).
我遇到了一些与此问题无关的问题,但在此过程中,我从类中的变量中删除了 @IBInspectable,并从身份检查器 (alt-apple-3) 中删除了属性。
After fixing the (code) issue with the component, I refreshed everything a ton of times, but still no attributes in the identity inspector.
修复组件的(代码)问题后,我刷新了所有内容很多次,但身份检查器中仍然没有属性。
Eventually, I noticed that they were back, but only in the attributes inspector (alt-apple-4). As soon as I added values to them there, they re-appeared in the identity inspector
最终,我注意到它们又回来了,但只是在属性检查器 (alt-apple-4) 中。一旦我在那里为它们添加值,它们就会重新出现在身份检查器中
回答by whycodewhyyyy
Dave Thomas's answerabove gave me the (reverse) solution when not of the others (Derived Data, Editor > Refresh) did, but for the sake of clarity in case people aren't sure where to edit the XML... you don't need to!
上面戴夫·托马斯的回答给了我(反向)解决方案,而其他人(派生数据,编辑器>刷新)没有这样做,但为了清楚起见,以防人们不确定在哪里编辑 XML......你不知道不需要!
- In your storyboard file select the troublesome view
- On the right-hand sidebar select the Identity Inspector tab (3rd option from the left).
- You'll have your custom class, which should already be set, and the
Module
. For me this was empty, and I was getting the same errors as OP. I set theModule
to my project nameand BAM - it started working after rebuilding!
- 在您的故事板文件中选择麻烦的视图
- 在右侧边栏上选择 Identity Inspector 选项卡(左起第三个选项)。
- 您将拥有应该已经设置好的自定义类,以及
Module
. 对我来说这是空的,我遇到了与 OP 相同的错误。我将 设置Module
为我的项目名称和 BAM - 它在重建后开始工作!
回答by Chuy47
I was having the same problem and I had to change the cornerRadius and BorderWidth to be a String and then cast it to CGFloat, it was the only solution for me to be able to change the values and see the changes in interface builder.
我遇到了同样的问题,我不得不将 cornerRadius 和 BorderWidth 更改为字符串,然后将其转换为 CGFloat,这是我能够更改值并查看界面构建器中更改的唯一解决方案。
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor!.CGColor
}
}
@IBInspectable var borderWidth: String? {
didSet {
layer.borderWidth = CGFloat(Int(borderWidth!) ?? 0)
}
}
@IBInspectable var cornerRadius: String? {
didSet {
layer.cornerRadius = CGFloat(Int(cornerRadius!) ?? 0)
layer.masksToBounds = layer.cornerRadius > 0
}
}