Xcode 開発でcornerRadiusを使って様々な形のボタンを作る方法

Xcode 開発(iosアプリ)で、円形ボタンを使用したいときの対処方法です。

Xcode 開発で対処可能な方法
1.ボタンの角丸の応用
2.丸いボタンの画像+透明色ボタンの組み合わせ

以下は、「1.ボタンの角丸の応用」の方法が容易で、対応方法をまとめました。

以下の画像は、 Xcode の標準で対応可能な方法です。


対応方法


ボタン1: Xcode の標準ボタン
ボタン2:角丸ボタン
ボタン3:楕円ボタン
ボタン4:円ボタン

これらの違いは、cornerRadiusの値の違いになります。

また、楕円と円の違いで、円はもとのボタンの設定が正方形でcornerRadiusの2倍の値を幅・高さにセットします。

ボタン1:bu1、ボタン2:bu2、ボタン3:bu3、ボタン4:bu4

1@implementation ViewController
2@synthesize bu1;
3@synthesize bu2;
4@synthesize bu3;
5@synthesize bu4;
6 
7- (void)viewDidLoad {
8    [super viewDidLoad];
9 
10    bu1.layer.cornerRadius = 0;
11    bu2.layer.cornerRadius = 10;
12    bu3.layer.cornerRadius = 30;
13    bu4.layer.cornerRadius = 30;
14     
15}
Verified by MonsterInsights