How to create a round button?

Tested Code:

.h

 #import <QuartzCore/QuartzCore.h>

-(void)roundButtonDidTap:(UIButton*)tappedButton;

.m

#define ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere

-(void)roundButtonDidTap:(UIButton*)tappedButton{

    NSLog(@"roundButtonDidTap Method Called");

}



UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

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

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

//width and height should be same value
button.frame = CGRectMake(0, 0, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT);

//Clip/Clear the other pieces whichever outside the rounded corner
button.clipsToBounds = YES;

//half of the width
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;

[self.view addSubview:button];

Result

enter image description here

Geometry in this concept

enter image description here


Storyboard Option (should apply to Swift or ObjC) -

If you prefer to work in Storyboards, there's another option.

First, set the width and height to be the same value to make a perfect square. enter image description here

Second, type in the following attributes. IMPORTANT- make the value of your layer.cornerRadius half the size of your width.

Then, when you run the app, your button will be round.

enter image description here


Try this, it worked for me. It will make your button or any view in circular shape only if you have taken it in square i.e width should be equals to height.

yourButton.layer.cornerRadius = yourButton.bounds.size.width/2;