简体   繁体   中英

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??

为什么不添加另一个UIView并将其添加到某个事件的UIView上?

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];

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM