Y2theZ
Y2theZ

Reputation: 10402

UIView change background transparency

I am working on an Iphone Application.

I am creating a UIView:

UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];

Then I want to use an image as a background for the view:

UIColor * bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_bg.png"]];

popupView.backgroundColor = bgColor;

Is there a way I can make that background semi transparent? maybe set the alfa value to 0.5 or give it an opacity.

I tried to make the image transparent using photoshop but when I set it as a background it is no longer transparent.

Note that I need only the background to be transparent not the subviews

Thanks a lot for any help

Upvotes: 3

Views: 9105

Answers (2)

Shashank Kulshrestha
Shashank Kulshrestha

Reputation: 1556

You can reduce alpha to 0.5 to make it transparent or change it as you required.

You can refer thisLINK

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

There is a simple trick for this. Add a subview of same frame and change it's alpha.

UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
UIView *bgView = [[UIView alloc] initWithFrame:popupView.frame];
UIColor * bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_bg.png"]];
bgView.backgroundColor = bgColor;
bgView.alpha = 0.5;
[popupView addSubview:bgView];

Upvotes: 8

Related Questions