Objective-c – Coloration of UIButtons of type UIButtonTypeRoundedRect

cocoa-touchcolorsiphoneobjective cuibutton

I would like the rounded rectangle portion of the subject type of UIButton to be a solid custom color I specify. I understand that

. setTitleColor : changes the text color
. backgroundColor : changes the color of the four pie-shaped corner pieces behind the rounded rectangle

The question is how to change the color of the rounded rectangle portion.

I have tried setImage, but the image has to have rounded corners, and is of no value when the button changes size. It does not scale to the new size.

Thanks in advance.

Best Answer

Try

CALayer *subLayer = [CALayer layer];
subLayer.frame = self.theButton.bounds;
subLayer.cornerRadius = 10.0;
subLayer.opacity = 1.0;

CALayer *imageLayer = [CALayer layer];
imageLayer.frame = subLayer.bounds;
imageLayer.cornerRadius = 10.0;


imageLayer.masksToBounds = YES;
imageLayer.opacity = 1.0;
imageLayer.contents = (id) [UIImage imageNamed:@"your.png"].CGImage;

[subLayer addSublayer:imageLayer];

[self.theButton.layer addSublayer:subLayer];
Related Topic