crazy2431 crazy2431 -4 years ago 143
Objective-C Question

how to create a round button?

I am new to Iphone development.

I want to create a round circlular button. This button should look like a circle.

This gives round rectangular button.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 30);
UIImage *img = [UIImage imageNamed:@"01.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];

I figured out how to create a rounded rectangular button but I want to create a round circle button. Please suggest.

Answer Source

Tested Code:


 #import <QuartzCore/QuartzCore.h>



#define ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere


    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

//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;

[self.view addSubview:button];


enter image description here

Geometry in this concept

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download