如何绘制背景具有 alpha 值但前景正常的自定义按钮
我正在尝试创建一个具有半透明背景和不透明前景的自定义按钮。我使用以下代码为自定义按钮创建背景图像:
UIImage *buttonBg = [UIImage imageNamed:@"ButtonBG_35"];
buttonBg = [buttonBg stretchableImageWithLeftCapWidth:14.0 topCapHeight:0];
CGSize size = self.button1.bounds.size;
UIGraphicsBeginImageContext( size );
[buttonBg drawInRect:CGRectMake( 0, 0, size.width, size.height ) blendMode:kCGBlendModeNormal alpha:0.33];
UIImage *button1BgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.button1 setBackgroundImage:button1BgImage forState:UIControlStateNormal];
这正如我在按钮中放置文本时所期望的那样工作。但是,如果我将图像放置在按钮前景中,则该图像也是半透明的。如何阻止前景图像变得透明。
我尝试使用 alpha:1.0 将图像绘制到按钮中,但这没有执行任何操作。
感谢您的任何帮助。
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:CGRectMake( 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是问题的直接解决方案,而是一种解决方法:将按钮保持为“空”,并将图像作为子视图添加到其中 - 同时将按钮的 alpha 保持为 1.0(或任何您想要的值)。
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).