views:

270

answers:

3

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.

A: 

Does using backgroundColor of UIButton's titleLabel work?

Adam Woś
Good idea, but no, it doesn't work. The titleLabel displays as a rectangle "inside" the UIButton. Making the font size of the related font large enough causes the titleLabel rectangle to exceed the size of the UIButton. It looks horrible.
McPragma
Then I'm afraid you'll have to use `UIButtonTypeCustom`...
Adam Woś
No problem doing that, Adam, but how does one get the rounded rectangle appearance on the button. Or are you saying one just can't ... it's either color or shape but not both?
McPragma
You need to have an image with rounded corners and use `imageEdgeInsets`.
Adam Woś
Thank you to all respondents. The complete answer for anyone else with this issue is you need to create rounded corner images and fit them into a rectangular button. The corners of the button image need to match the color of your background ... tricky if the background is not solid. Your images also need to include your "title" as part of the image.
McPragma
A: 

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.

Matt Long
A: 

I think that the easiest way to do this would just be to create a solid sounded 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...

Dwaine Bailey