1

過去に私は次のことをしました:

thumbnailButton = [[UIButton alloc] initWithFrame: thumbnailButtonFrame];
[thumbnailButton addTarget:self action:@selector(thumbnailButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
[thumbnailButton setBackgroundImage: thumbnailButtonImage forState: UIControlStateNormal];
[thumbnailButtonImage release];

これにより、サムネイル画像が表示されたボタンが作成され、押すと灰色のハイライトが表示されます(サムネイルのリストから表示する写真を選択するときの写真アプリのように)。

UIViewメソッドにグラフィックを描画するクラスがあるというシナリオがありますdrawRectUIViewこれをに付けUIButtonて、画像を追加した場合と同じように強調表示させたいと思います。サブクラス化せずにこれを実行したいと思いUIButtonます。これは可能ですか?

4

2 に答える 2

0

このアプローチを試すことができます..通常の状態と強調表示された状態の 2 つの画像を持つことができます..

btnImage = [UIImage imageNamed:@"buttonInactiveImage.png"];
btnImageSelected = [UIImage imageNamed:@"buttonActiveImage.png"];
btn = [[UIButton alloc] initWithFrame: thumbnailButtonFrame];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[btn setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button

この方法では、ボタンにビューを配置する必要はありません

于 2012-10-12T08:53:16.217 に答える