开发者

how to make button round with image background in IPhone?

I have a round button, but i will make it have a backgroud with image.

but when i do it, by setting the (what) property to an image, the button开发者_JAVA百科 becomes rectangle because the image is a rectangle. How do I keep the button round?


Simply by doing this

#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;


Tested Code:

.h

    -(void)vijayWithApple; 

.m

   -(void)vijayWithApple{

            NSLog(@"vijayWithApple Called");
   }


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"Micky.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Show View" forState:UIControlStateNormal];

button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value

button.clipsToBounds = YES;

button.layer.cornerRadius = 20;//half of the width

button.layer.borderColor=[UIColor redColor].CGColor;

button.layer.borderWidth=2.0f;

[self.view addSubview:button];

Restut

how to make button round with image background in IPhone?


Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜