开发者

How to draw a custom button with background having an alpha value but foreground being normal

I am trying to create a custom button with a semi-transparent background and a non-transparent foreground. I am using the following code to create a background image for the custom button:

UIImage *buttonBg = [UIImage imageNamed:@"ButtonBG_35"];
buttonBg = [buttonBg stretchableImageWithLeftCapWidth:14.0 topCapHeight:0];

CGSize size = self.button1.bounds.size;

UIGraphicsBeginImageContext( size );

[buttonBg drawInRect:C开发者_如何学PythonGRectMake( 0, 0, size.width, size.height ) blendMode:kCGBlendModeNormal alpha:0.33];
UIImage *button1BgImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.button1 setBackgroundImage:button1BgImage forState:UIControlStateNormal];

This works as I expect when placing text in the button. However, if I place an image in the button foreground then that image is also semi-transparent. How do I stop the foreground image from being transparent.

I tried to draw the image into the button using an alpha:1.0 but that didn't do anything.

Thanks for any help.


Not a direct solution to your problem, but rather a workaround: Keep the button "empty", and add the images as subviews to it - while keeping the button's alpha at 1.0 (or whatever you want it to be).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜