UIView背景图片透明度问题
我添加了一个带有 PNG 图像的子视图,该图像应该以透明度显示,但我在透明度应该是的地方变得全黑。
代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BannerBackground.png"]];
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Logo.png"]];
[self.view addSubview:logoImage];
}
这是适用于 iPad、iOS 版本 4.3 的代码。我在模拟器和 iPad 上都遇到同样的问题。
由于这是最后添加的视图,我希望它的 az 索引高于所有其他视图,因此其他视图应通过透明区域显示。这是行不通的。
I added a subview with an PNG image that should display with transparency, but I am getting all black where the transparency should be.
The code is:
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BannerBackground.png"]];
logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Logo.png"]];
[self.view addSubview:logoImage];
}
This is code for a iPad, iOS version 4.3. I have the same problem with both the simulator and the iPad.
Since this is the last view added, I expect it to have a z index higher than all of the other views, so the other views should show through the transparent areas. This is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我注意到这在 iOS 4.3.x 上的行为与 5.0 不同.x。在 4.3.x 中,我必须将不透明设置为 YES,然后设置背景图像,然后将其设置回 NO。
I noticed this behaves differently on iOS 4.3.x than 5.0.x. In 4.3.x I had to set opaque to YES, then set the background image, then set it back to NO.
将视图的属性不透明设置为否。像这样 [view setOpaque:NO];
Set your view's property opaque to no. like this [view setOpaque:NO];
失败 setOpaque:NO...检查您的图像是否正确导出并且没有损坏等...这发生在我身上一次
Failing setOpaque:NO... check to see if your image is correctly exported and not corrupted etc.. that happened to me once
创建一个具有透明背景颜色和您的徽标作为图像的 UIImageView,并添加:
Create a UIImageView with transparent backgroundColor and your logo as its image, and add that:
这应该有效。
This should work.
老问题,但是:你的透明度是否有可能是黑色的,而不是真正透明的?并不是说我刚刚做了那件事或其他什么事情。
(绘制工具栏图标时,iOS 会用白色填充所有像素,包括黑色像素。确保您期望的透明区域实际上没有任何内容。)
Old question, but: any chance that your transparency is, for instance, black and not actually transparent? Not that I just did that or anything.
(When drawing the toolbar icon, iOS fills in any pixels, including black ones, with white. Make sure there's actually nothing where you expect your transparent area to be.)