Akash Malhotra
Akash Malhotra

Reputation: 1106

Set alpha of a portion of a UIView

I need to make a rectangular portion of my view blacked out when the user taps the SearchBar. I cant figure out a way to do that. I tried CGRectMake but how do i set alpha of that rect??

Upvotes: 1

Views: 284

Answers (3)

Exploring
Exploring

Reputation: 935

It can be done by adding sublayers to the layer of your view in the following way

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(50.0, 0.0, 100.0, 100.0)] autorelease]; view.backgroundColor = [UIColor grayColor]; [self.view addSubview:view];

CALayer *layerUP = [CALayer layer];
[layerUP setFrame:CGRectMake(0.0, 0.0, 100.0, 50.0)];
[layerUP setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor];
[view.layer addSublayer:layerUP];

CALayer *layerDown = [CALayer layer];
[layerDown setFrame:CGRectMake(0.0, 50.0, 100.0, 50.0)];
[layerDown setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2].CGColor];
[view.layer addSublayer:layerDown];

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

Add an UIImageView with a black background:

    UIImageView *myView= [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    myView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:myView];
    [myView release];

Upvotes: 1

Yama
Yama

Reputation: 2649

Why don't you add another UIView and add it on the UIView at some event?

Upvotes: 3

Related Questions