Coloring UIButtons of type UIButtonTypeRoundedRect

I would like the rounded rectangular part of an object of type UIButton to be the solid custom color that I specify. I understand that

. setTitleColor: changes the color of the text, backgroundColor: changes the color of the four circular corner shapes behind the rounded rectangle

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

I tried setImage, but the image should have rounded corners and it doesn't matter when the button resizes. It does not scale to a new size.

Thanks in advance.

+3
source share
4 answers

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];
+2
source

. , , .

[[button layer] setCornerRadius:12.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];

QuartzCore ( #import), .

, "". , ​​: UIButton * button = [[UIButton buttonWithType: UIButtonTypeCustom] ];

+1

backgroundColor UIButton titleLabel?

0

, - png , .

.

Other than that, I really don't think it can be done ... Not sure ...

0
source

Source: https://habr.com/ru/post/1728954/


All Articles