一旦画像ファイルからUIImageに変換し、ボタン作成時にUIImageを指定して作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// ボタン画像用のUIImageを生成
UIImage *buttonImg = [UIImage imageNamed:@"button.png"];
// ボタンサイズを指定
UIButton *imgBtn = [[[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 100, 44)] autorelease];
// 画像をセット
[imgBtn setBackgroundImage:buttonImg forState:UIControlStateNormal];
// ボタンが押された時にopenメソッドを呼び出すように指定
[btn addTarget:self action:@selector(open:)
forControlEvents:UIControlEventTouchUpInside]; |
「setBackgroundImage: forState:」メソッドのforStateには
1,UIControlStateNormal(ボタンが有効な時)
2,UIControlStateHighlighted(ボタンがハイライトの時)
3,UIControlStateDisabled(ボタンが無効な時)
と、3種類の状態が指定できるので、それぞれの状態に対して異なる画像を指定できます。
Related posts: