trouble with UIAppearance and UIButton subclassing

送分小仙女□ 提交于 2019-12-12 02:34:51

问题


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.

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.

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?


回答1:


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 will be a sub-class of 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.



来源:https://stackoverflow.com/questions/18107492/trouble-with-uiappearance-and-uibutton-subclassing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!