how to make button round with image background in IPhone?
Asked Answered
C

3

4

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 becomes rectangle because the image is a rectangle. How do I keep the button round?

Cooksey answered 14/6, 2011 at 2:50 Comment(1)
Why dont you use a round image ?Buckbuckaroo
C
10

Simply by doing this

#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
Caseose answered 14/6, 2011 at 2:56 Comment(3)
also add myButton.clipsToBounds = YES;Dade
@Dade Thanks for the clipToBound hint. That was the missing link. When using storyboards, mark "Clip Subviews" (and add layer.cornerRadius to Runtime Attributes).Millesimal
@Millesimal I didn't know about the storyboard trick! Well, you learn things every day.....Thanks!Dade
P
0

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

enter image description here

Potpie answered 5/8, 2011 at 9:19 Comment(0)
M
0

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

Mauer answered 23/11, 2017 at 21:40 Comment(1)
an example would be helpful. links to resources is also good.Methadone

© 2022 - 2024 — McMap. All rights reserved.