开发者

Background color for subclassed UIButton?

I have a UIButton subclass that I initialize as:

MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];

I want to set the background color of this button.

If I do the following in the view controller after creating the button:

MyButton.backgroundColor = [UIColor ...];

It works fine. But if I try to do the following within the UIButton subclass, either in initWithFrame or in DrawRect:

self.backgroundColor = [UIColor ...];

It simply does nothing. The button remains transparent.

The thing is, my logic is such that I really need to set 开发者_如何学Gothe color inside the UIButton subclass, not in the calling code.

Any ideas?


UIButton is not meant to be subclassed. It is a class cluster and you almost certainly get it wrong (it may break now or in the future). The base method buttonWithType: also will never return an instance of your class, so you need to go to great lengths to make all your code work.

The much better way is to to make a factory method that uses UIButton.buttonWithType: and configures the button the way you need it.


Can you implement "Buttonwithtype" method in your subclass and write code there self.backgroundColor = [UIColor WhateverColor];

It should work. let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜