IOS custom button is similar to barbutton button

I would like to place buttons in different places (not in the navigation bar) that have dynamic text. I want them to look like elements of a black navigation bar (with a gray and black gradient).

How should I do it? They should be dynamic width based on the button text. I know I can create png files and stretch them, but is there a better way?

+5
source share
1 answer

You need to create a button yourself using images. Just create a custom UIButton and assign the appropriate images to the different states of the buttons that interest you.

UIImage stretchableImageWithLeftCapWidth , , NSString sizeWithFont .

http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight:

- :

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

int capHeight = 31;
int capWidth = 9;
UIImage *buttonTemplate = [UIImage imageNamed:@"button_template.png"];
UIImage *stretchedButtonImage = [buttonTemplate stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
[button setBackgroundImage:stretchedButtonImage forState:UIControlStateNormal];
+5

All Articles