简体   繁体   中英

iPhone Can subview be transparent?

I have main view and i added subview to it. I set subview's background to be semi transparent, but subview is drawn in solid color. I even tried setting opaque to NO.

detailsView.opaque = NO;
detailsView.backgroundColor = UIColorMakeRGBA(0, 0, 100, 100);

I can make the vsubview to be transparent via alpha property (but all things on subview will also be transparent), but i really want just to set background color to be semi transparent.

Set alpha to desired value in colorWithRed:green:blue:alpha method

detailsView.backgroundColor = [UIColor colorWithRed:0/255.0f 
                                              green:30/255.0f 
                                               blue:160/255.0f 
                                              alpha:0.5f];

I don't know the UIColorMakeRGBA function, but it looks to me like you are creating a completely blue colour with alpha of 1, so it should not be transparent. Try setting the background colour line to be

detailsView.backgroundColor = UIColorMakeRGBA(0, 0, 100, 50);

Alternatively just use [[UIColor blueColor] colorWithAlphaComponent:0.5f];

I assume UIColorMakeRGBA is a function that you have implemented. In that case, I am sure, it looks like this:

#define UIColorMakeRGBA(redValue, greenValue, blueValue, alphaValue) [UIColor colorWithRed:(redValue)/255.0f green:(greenValue)/255.0f blue:(blueValue)/255.0f alpha:(alphaValue)]

Notice that alpha is not divided by 255.0 , so you should use a value between 0.0 and 1.0 for that parameter and not 100 . If you use 100, you will end up with a color with alpha equal to 1.0 .

detailsView.backgroundColor = UIColorMakeRGBA(0, 0, 100, 0.5f);

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