IB_DESIGNABLE,IBInspectable - 接口构建器不更新 [英] IB_DESIGNABLE, IBInspectable -- Interface builder does not update

查看:135
本文介绍了IB_DESIGNABLE,IBInspectable - 接口构建器不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一组代码:

#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



CustomView.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






(对于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
        }
    }
}






我添加了 UIView 到故事板上的视图控制器并将其子类设置为 CustomView


I added a UIView to a view controller on the storyboard and set its subclass to CustomView.

这会添加Designables行。它停留在正在更新,工具提示显示等待目标构建。它永远不会从这个状态改变。

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.

当我转到属性inspect时,我能够设置这些 IBInspectable 属性:

When I move to the attributes inspect, I am able to set these IBInspectable properties:

一旦设置,它们也会显示在用户定义的运行时属性中:

And once set, they also show up in the "User Defined Runtime Attributes":

但是,Designables状态永远不会超出更新仍然使用相同的工具提示(我已多次尝试Cmd + B构建,没有任何更改)。

However, the "Designables" status never moves beyond "Updating" with still the same tooltip (I've tried Cmd+B building several times, nothing changes).

此外,我设置 IBInspectable 属性,我收到每个属性的警告:

Moreover, as I set the IBInspectable properties, I get a warning for each one:


IBDesignables - 忽略用户为UIView实例上的键路径borderColor定义的运行时属性...此类不是键值borderColor的键值编码。

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.

生成警告的屏幕截图:

我熟悉符合键值编码的问题,并且通常知道如何解决这些问题。 ..但我不明白如何在这里解决这个问题。根据视图的身份检查器,视图是CustomView(不是常规的UIView,它没有这些属性)。如果视图不是CustomView,那么这些可设计的属性将不会出现在Attributes Inspector中,对吧?但是当Interface Builder尝试将这些属性应用于视图时,它会回过头来认为视图的类是UIView并且不能应用属性。

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.

任何帮助?如果我遗漏了一些重要的细节,请告诉我,但是为了它的价值,我完全按照本教程(除了ObjC vs Swift)。还值得注意的是,我在另一台机器上完全按照本教程进行操作,它就像一个魅力(我打算昨晚发布这个帖子,但我当时的电脑没有这个问题)。

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).

根据评论,有人建议不要包含 .m 文件这可能会导致问题。我当然认为我会为这种情况做好准备,但无论如何我都会检查。

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.

当我第一次尝试这样做时,我的理解是 IB_DESIGNABLE 类必须是另一个 UIKit 框架的一部分。因此,从第一个屏幕截图中,您可以看到我设置了一个CustomViews框架,它有一个类 CustomView 。你还会在这里看到我还创建了一个 OtherView ,它与 CustomView 完全相同,除了它不在单独的框架。但是,两个类之间的故事板上仍然存在相同的问题。

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.

这里我们有一个屏幕截图,指示 CustomView.m 包含使用 CustomViews 框架构建:

Here we have a screenshot indicating that CustomView.m is included to be built with the CustomViews framework:

同时,以下屏幕截图显示了几件事:

Meanwhile, the following screenshot indicates several things:


  • CustomViews.framework 适当地包含在主项目中。

  • OtherView.m 也作为编译源包含在内,所以即使 CustomView 出现问题, OtherView 应该有效,但它会产生相同的错误。

  • Main.storyboard LaunchScreen.xib 显示为红色。我不知道为什么,并且没有丝毫的线索,为什么 LaunchScreen.xib 应该(我没有触及过这个文件),虽然我可以在看完之后说其他项目, Main.storyboard 也会以红色显示这些项目,而且我没有做任何事情 IB_DESIGNABLE IBInspectable 那里。

  • CustomViews.framework is appropriately included in the main project.
  • OtherView.m is also included as a compile source, so even if something is wrong with CustomView, OtherView should work, however it generates identical errors.
  • Main.storyboard and LaunchScreen.xib are showing up as red. I have no idea why, and haven't the slightest clue as to why LaunchScreen.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 with IB_DESIGNABLE or IBInspectable there.

我试过了现在重试了好几次。它每次都在家里的电脑上工作 - 我不能在家里重现这个问题中描述的问题。在工作中,它永远不会奏效。这个问题中描述的问题每次都会发生。

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.

两台计算机都是今年新推出的Mac Minis(不是新机型,2012年末型号)。两台计算机都运行OS X Yosemite 10.10。两台计算机都运行Xcode版本6.1。在家里,建筑是(6A1052d)。今天早上,我可以确认两台计算机都运行相同的Xcode版本。

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.

其他人向我建议可能是RAM不好。这对我来说似乎遥不可及。我多次重启项目,多次重启计算机。对我来说,如果大约6个月大的计算机上存在坏RAM,我会看到其他问题,而且这个问题会不那么一致。但是这个确切的问题仍然存在,尽管多次从头开始重新启动整个项目并在计算机上完全重启。

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.

它应该是值得注意的是,如果我实际编译并运行此项目,带有 IBInspectable 属性的自定义视图实际显示为我希望故事板显示它。我想,即使没有 IB_DESIGNABLE IBInspectable 指令也是如此,因为这些指令是作为用户创建的定义的运行时属性。

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.

推荐答案

基于chrisco建议调试所选视图(我已经完成了,但又去了为了获得良好的衡量标准),我注意到编辑器菜单底部有几个其他选项。

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.


  • 自动刷新视图

  • 刷新所有视图

我点击了刷新所有视图并在Xcode有点思考之后突然故事板按预期显示我的视图(正确应用我的 IBInspectable 属性)。

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).

然后我再次完成整个过程以确认这是解决方案。

I then went through the whole process again to confirm that this is the solution.

我创建了一个新类, Thir DVIEW 。这个类再次与其他类完全相同。我将我的视图类更改为 ThirdView 并且这次略有不同:

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:

点击显示给我的警告:

这次是一个新的:


对于具有自定义类的对象使用类UIView,因为类ThirdView没有存在。

Using class UIView for object with custom class because the class ThirdView does not exist.

这实际上并不比已经存在的更有帮助。另外,现在其他三个警告已经奇怪地翻了六倍。

This isn't really any more helpful than what already existed. Plus, now the other three warnings have doubled into 6 strangely.

无论如何,如果我再次点击编辑器下拉菜单中的刷新所有视图,所有错误都会消失离开,再次,视图正确显示。

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.

到目前为止,我所做的一切都是我在家里从未搞砸过的东西。在家里,它只是工作。所以我打开了自动刷新视图并创建了一个FourthView进行测试 - 再次与前三个相同。

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.

将视图的类更改为 FourthViewthe designables标签说更新片刻然后最后说最新:

After changing the view's class to "FourthView" the designables label said "Updating" for a short moment then finally said "Up to date":

所以,我在家检查了我的电脑。在始终有效的计算机上打开自动刷新视图。它在计算机上被关闭了。我永远不记得触摸这个菜单选项了。我甚至无法确定它是否存在于Xcode 6之前。但是这个选项正在产生差异。

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.

TL; DR,如果您遇到问题中描述的相同问题,请确保打开自动刷新视图(或在IB中需要更新时手动刷新所有视图):

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):

这篇关于IB_DESIGNABLE,IBInspectable - 接口构建器不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆