UIAppearance和UIButton子类化的问题 [英] trouble with UIAppearance and UIButton subclassing

查看:160
本文介绍了UIAppearance和UIButton子类化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义按钮,它只是一个标准的UIButton,但添加了C​​AGradientLayer。

I have a custom button, that is just a standard UIButton, but with a CAGradientLayer added in.

在我的自定义按钮中,我定义了两个属性:

In my custom button, I have defined two properties:

@property (nonatomic, strong) UIColor* topColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor* bottomColor UI_APPEARANCE_SELECTOR;

如果设置了这两个值,我的按钮会使用一个漂亮的线性渐变来绘制自己。效果很好。

If those two values get set, the my button draws itself with a nice linear gradient. Works great.

我也想尽可能多地使用InterfaceBuilder。因此,在其中一些按钮上,在IB的Identity Inpsector中,我为这些属性添加了用户定义的运行时属性。再次,效果很好。

I also like to put as much into InterfaceBuilder as possible. So, on some of these buttons, in IB's "Identity Inpsector" I add in "User Defined Runtime Attributes" for these properties. Again, works great.

接下来,我想我会尝试使用UIAppearance代理。我的大多数自定义渐变按钮都具有相同的颜色。但有一些是不同的。所以,我想我要做的是使用外观代理的东西来设置这个类的默认颜色,然后对于任何不同的按钮,我可以在IntefaceBuilder中设置它们的值。这失败。

Next, I thought I'd try using UIAppearance proxies. Most of my custom gradient buttons all have the same colors. But there are a few that are different. So, I figured what I would do is use the appearance-proxy stuff to set the default colors for this class, and then for any buttons that are different, I could just set their values in IntefaceBuilder. This fails.

显然,正在发生的事情是它首先从我的故事板文件中读取运行时属性,但之后这些值会被外观代理覆盖。我不希望这种方式起作用,但确实如此。

Apparently, what's happening is that it's reading the runtime attributes from my storyboard file first, but afterwards those values get overwritten by the appearance proxy. I wouldn't expect this to work this way, but it does.

关于如何实现这一目标的任何提示?或者我应该放弃运行时属性的东西?

Any tips on how to accomplish this? Or should I just give up on the runtime attributes thing?

推荐答案

好的,我已经考虑过了,我猜这个实际上是Appearance代理应该做的事情。所以,我的解决方案是有两个类MySpecialButton和MyAppearanceButton。

OK, I've thought about this, and I guess this is really what the Appearance proxy is supposed to do. So, my solution is to have two classes "MySpecialButton" and "MyAppearanceButton".

MyAppearanceButton将是MySpecialButton的一个子类。

MyAppearanceButton will be a sub-class of MySpecialButton.

MyAppearanceButton的外观将由外观代理调用控制。如果我想要一个不受这种方式控制的按钮,我将创建一个MySpecialButton并在用户定义的运行时属性中设置属性。应该这样做。

The look of "MyAppearanceButton" will be controlled by the appearance proxy calls. If I want a button that isn't controlled that way, I'll make a "MySpecialButton" and set the properties in the User Defined Runtime Attributes. That should do it.

这篇关于UIAppearance和UIButton子类化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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