Flipkart Search

Search This Blog

Friday, May 1, 2009

Create UIButton/button with images

UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);

[playButton setTitle:@"Play" forState:UIControlStateNormal];

playButton.backgroundColor = [UIColor clearColor];

[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];

UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];

UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];

[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];

UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];

UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];

[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];

[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:playButton];


Anonymous said...


Anonymous said...

Hi, Nice tutorial. Is it possible to make a button's size smaller than 20x20? I'm not having much luck with that.


Matthew Cawley said...

Good Post, make sure you release any retained objects at the end of your code though or you will create a potential memory leak

[playButton release];

you don't need to release the images as you do not directly allocate memory space for them or retain them so they are auto released.

Anonymous said...

thanks! mad helpful

gopal said...

nice code..thanks

Joseph said...

Thanks for this code snippet. It worked great!

Joseph said...
This comment has been removed by the author.