thanhdevapp
thanhdevapp

Reputation: 69

How do I make a circular button?

*I have this answers here : Correct with help of **Yossi How to put buttons over UITableView which won't scroll with table in iOS***

I know answers work with NavigationControllerController

[self.navigationController.view addSubview:_btnCircle];

I learned to write in Objective C iOS app. Please help me to implement to get the Circle button display on uitableview as shown below. Or help me search keywords.

Example 1:

enter image description here

Example 2:

enter image description here

Upvotes: 3

Views: 10490

Answers (3)

pableiros
pableiros

Reputation: 16052

For Swift:

button.layer.masksToBounds = true
button.layer.cornerRadius = self.frame.width / 2

If you are creating a custom UIButton (or a custom UIView), implement the following inside the custom class:

override func layoutSubviews() {
    super.layoutSubviews()
    self.layer.masksToBounds = true
    self.layer.cornerRadius = self.frame.width / 2
}

Upvotes: 0

malex
malex

Reputation: 10096

The simplest way is to add QuartzCore Framework

#import <QuartzCore/QuartzCore.h>

and then use the following code for your button:

button.layer.cornerRadius = 0.5 * button.bounds.size.width;

Now you have the round button from the square one.

Upvotes: 12

Shubhendu
Shubhendu

Reputation: 1091

In order to make any button round you need to set the cornerRadius property of the button.

Note- In order to make circle you need to make sure that height and width are equal and radius is set half of the width/height. This will make perfect round circle.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(10, 10, 50, 50)];
btn.layer.cornerRadius = 0.5 * btn.bounds.size.width;

Upvotes: 4

Related Questions