[Objective-C] 関数リファレンス #UIButton

2015年10月3日

Objective-C テクノロジー プログラミング

画面内でクリックイベントを取得する為にボタンを配置する。 装飾やアクションなども柔軟に設置できるので、かなり広範囲に使用できます。 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *btn = [[UIButton alloc] init]; btn.frame = CGRectMake(100,100,100,100); btn.text = @"Button-Test"; btn.layer.borderWidth = 2; btn.layer.borderColor = [[UIColor redColor] CGColor]; btn.textAlignment = NSTextAlignmentCenter; btn.numberOfLines = 3; btn.textColor = [UIColor redColor]; [self.view btn]; }

プロパティ

初期設定

UIButton *btn = [UIButton buttonWithType:*type*]; - type UIButtonTypeRoundedRect UIButtonTypeContactAdd UIButtonTypeDetailDisclosure UIButtonTypeInfoLight UIButtonTypeInfoDark UIButtonTypeCustom

setTitle

# タイトルの文字列を設定 [*ボタン要素* setTitle:@"*タイトル名*" forState:*state*]; # state UIControlStateNormal(有効) UIControlStateHighlighted(ハイライト) UIControlStateDisabled(無効)

setTieleColor

# タイトルカラーの設定 [*ボタン要素* setTitleColor:[UIColor *色*] forState:*state*]; # color [UIColor *color*](ex)redColor,whiteColor,blackColor,blueColor... [UIColor colorWithRed:*0.0 - 1.0* green:*0.0 - 1.0* blue:*0.0 - 1.0* alpha:*0.0 - 1.0*]

enabled

# ボタンの有効・無効を切り替える[ YES / NO ] btn.enabled = YES;//有効 [btn setEnabled:NO];//無効

backgroundColor

# 背景色を設定 btn.backgroundColor = [UIColor orangeColor];

alpha

# 透明度の設定 [ 0.0 - 1.0 ] btn.alpha = 0.5;

hidden

# 非表示 [ YES(非表示) / NO(表示) ] btn.hidden = YES;

tag

# 数値プロパティのセット※任意情報として使用できる btn.tag = 100;

frame

frame # オブジェクトのサイズ、位置などを指定。(CGRectMake) 位置[x,y]、サイズ[w,h] *ボタン要素*.frame = CGRectMake(50,50,100,150);

layer

layer.borderWidth # 枠線の幅 *ボタン要素*.layer.borderWidth = 4; layer.orderColor # 枠線の色 *ボタン要素*.layer.orderColor = [UIColor *色*]; layer.cornerRadius # 角丸 *ボタン要素*.layer.cornerRadius = 10.0;

currentTitle

# タイトル文字列を取得 NSString *name = btn.*ボタン要素*.currentTitle;

currentTitle

# タイトル色を取得 NSString *name = btn.*ボタン要素*.currentTitleColor;

currentTitle

# 背景画像を取得 NSString *name = btn.*ボタン要素*.currentBackgroundImage;

currentTitle

# 画像を取得 NSString *name = btn.*ボタン要素*.currentImage;

サンプル

ボタンを押した時にタイトルを変更する

// 通常時 [btn setTitle:@"通常" forState:UIControlStateNormal]; // push時 [btn setTitle:@"PUSH" forState:UIControlStateHighlighted]; // 無効時 [btn setTitle:@"無効" forState:UIControlStateDisabled];

背景画像を表示する※事前にxcodeに画像を取り込んでおく

// 通常時 [btn setBackgroundImage:[UIImage imageNamed:@"bg_1.png"] forState:UIControlStateNormal]; // push時 [btn setBackgroundImage:[UIImage imageNamed:@"bg_3.png"] forState:UIControlStateHighlighted]; // 無効時 [btn setBackgroundImage:[UIImage imageNamed:@"bg_0.png"] forState:UIControlStateDisabled]; ** 背景画像のサイズはデフォルトでボタン要素の幅にfitする

画像をセットする

// 通常時 [btn setImage:[UIImage imageNamed:@"img_1.png"] forState:UIControlStateNormal]; // push時 [btn setImage:[UIImage imageNamed:@"img_3.png"] forState:UIControlStateHighlighted]; // 無効時 [btn setImage:[UIImage imageNamed:@"img_0.png"] forState:UIControlStateDisabled]; ** 背景画像のサイズはデフォルトでボタン要素の幅にfitする

ボタンを押した時に関数を実行する

[btn addTarget:self action:@selector(btnPush:) forControlEvents:UIControlEventTouchUpInside]; -(void)btnPush:(id)sender{ UIButton *btn = sender; NSLog(@"%@",btn.currentTitle); } ※ senderにはUIButtonの要素が入っているので複数ボタンがある場合は、要素の判別を行うことができる。

注意点

1、プロパティにstateを付ける書き方をしないとエラーになるので要注意。 2、ボタンを押した時に値を直接渡すことが出来ない。  ※やり方はいくつかあるが、本ページでは割愛 3、タイトル文字と画像は共存できないので、後書き有効になる。

その他

調査段階ではiOS9のバージョンです。 当ページは、storyboardを使わなずに、プログラムで使用する方法のみ記載しています。

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。