iPhone開発 UIButton カスタマイズできない時の解決方法。 ios 逆引き サンプル
UIButton をカスタマイズする場合、
バックグラウンドの色、バックグラウンドイメージ
などが、プログラムで指定しているのにもかかわらず、
変更できずにハマる方を何度か見かけましたので、
おさらいします。ただし、UIButtonTypeRoudedRect の場合、
テキストカラー等は変更できます。
インスタンスを作成時に、下記のコンビニエンスメッソドにて初期化しますが、
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
ハマっている方のコードを参照すると、
buttonWithType を 下記の引数で初期化しています。
初期化する値を再度おさらいしましょう。
下記それぞれ初期化イメージになります。
UIButtonTypeRoudedRect

UIButtonTypeContactAdd

UIButtonTypeDetailDisclosure

UIButtonTypeInfoLight

UIButtonTypeInfoDark

こちらで指定して初期化した場合は、ボタンをカスタマイズする、プロパティを
変更することができません。UIButtonTypeRoudedRect のテキストプロパティを除く。
もう一度、コードを確認して、UIButtonTypeCustom になているかを確認してください。
サンプル
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"TEST" forState:UIControlStateNormal];
button1.frame = CGRectMake(10, 10, 50, 30);
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
button2.frame = CGRectMake(0, 100, 50, 30);
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button3.frame = CGRectMake(0, 200, 50, 30);
[self.view addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeInfoLight];
button4.frame = CGRectMake(0, 300, 50, 30);
[self.view addSubview:button4];
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeInfoDark];
button5.frame = CGRectMake(0, 400, 50, 30);
[self.view addSubview:button5];
UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
button6.frame = CGRectMake( 100, 4, 157, 30 );
[button6 setTitle:@"TEST" forState:UIControlStateNormal];
button6.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
button6.titleLabel.textAlignment = UITextAlignmentCenter;
button6.titleLabel.textColor = [UIColor blackColor];
button6.titleLabel.backgroundColor = [UIColor clearColor];
button6.showsTouchWhenHighlighted = YES;
button6.backgroundColor = [UIColor redColor];
[self.view addSubview:button6];
