【发布时间】:2011-06-27 09:55:44
【问题描述】:
我有一个 UIButton 子类,我将其初始化为:
MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];
我想设置这个按钮的背景颜色。
如果我在创建按钮后在视图控制器中执行以下操作:
MyButton.backgroundColor = [UIColor ...];
它工作正常。但是,如果我尝试在 UIButton 子类中执行以下操作,无论是在 initWithFrame 还是在 DrawRect:
self.backgroundColor = [UIColor ...];
它什么也不做。按钮保持透明。
问题是,我的逻辑是,我确实需要在 UIButton 子类中设置颜色,而不是在调用代码中。
有什么想法吗?
【问题讨论】:
-
在drawRect中,你调用了[super drawRect]吗?如果不尝试调用它,然后做其他事情。
-
是的,我总是这样做 [super drawRect]。正如我所说,没有背景颜色。