2013年3月29日金曜日

背景のみを透過させたUIImageViewの作り方

いきなりiOSのテクニックメモ
iOSでUIImageViewの背景のみを透過させる方法について。

以下のようにすると、画像も透過していしまいます。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxxx"]];
imageView.backgroundColor = [UIColor grayColor];
imageView.alpha = 0.5;

imageView.alphaがUIImageView全体に効いてしまうからです。

そこで、UIColorで透過の色を作成し、
それをbackgroundColorに設定することで、
背景のみを透過にすることができます。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxxx"]];
UIColor *color = [UIColor grayColor];
UIColor *alphaColor = [color colorWithAlphaComponent:0.5];
imageView.backgroundColor = alphaColor;

0 件のコメント:

コメントを投稿