Reputation: 2208
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 the color inside the UIButton subclass, not in the calling code.
Any ideas?
Upvotes: 0
Views: 705
Reputation: 25632
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.
Upvotes: 1
Reputation: 4577
Can you implement "Buttonwithtype" method in your subclass and write code there self.backgroundColor = [UIColor WhateverColor];
It should work. let me know.
Upvotes: 0