Coloration of UIButtons of type UIButtonTypeRoundedRect
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.
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];
You can try to grab the layer and set its corner radius in a custom button. I haven't tried it, but it seems worth a try.
[[button layer] setCornerRadius:12.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];
You'll have to add the QuartzCore framework to your project (and #import ) in order to take advantage of these methods.
Just an addition, in this case you use button type as "Custom". So the button can be declared as : UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
Does using backgroundColor
of UIButton
's titleLabel
work?
I think that the easiest way to do this would just be to create a solid rounded button png image of your own, with transparencies around the rounded corners.
Then just set the background of your custom button to be that image that you created.
Other than that, I don't actually think that this is can be done...Not sure...
精彩评论