Flipkart Search

Cari Blog Ini

Jumaat, 1 Mei 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];

8 ulasan:

Tanpa Nama berkata...

thanks!

Tanpa Nama berkata...

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

Thanks

Matthew Cawley berkata...

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.

Tanpa Nama berkata...

thanks! mad helpful

gopal berkata...

nice code..thanks

Joseph berkata...

Thanks for this code snippet. It worked great!

Joseph berkata...
Ulasan ini telah dialihkan keluar oleh pengarang.
Haley Woods berkata...

I like how you customized the play button with images and colors.