代码之家  ›  专栏  ›  技术社区  ›  msmithstubbs

如何在使用colorWithPatternImage时保持图像透明度:

  •  17
  • msmithstubbs  · 技术社区  · 14 年前

    我有一张透明的图片,像这样:

    alt text

    使用两个ui视图,我将背景颜色配置为:

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];
    

    我希望在保留透明度的情况下,在UIView上重复使用红色正方形,但它是由如下纯色填充的:

    alt text

    我不明白为什么。有没有一个简单的方法来绘制一个透明的平铺图像?或者我需要看绘制核心图形模式?

    3 回复  |  直到 14 年前
        1
  •  25
  •   Joshua Weinberg    14 年前

    模式图像应该保持良好的透明度。

    尝试 [self.dashedView setOpaque:NO]

        2
  •  5
  •   Hulk_SMASH    12 年前

    在设置colorWithPatternImage之后,您只需将opaque设置为NO。

    self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];
    self.dashedView.opaque = NO;
    
        3
  •  3
  •   seenickcode    13 年前

    是的,谢谢你的反馈。

    我发现这只在iOS4.3.x上发生,而在iOS5.0.x上没有。所以在4.3.x上,我必须执行Yar id并将opaque设置为NO,然后将background image设置为YES。

    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.opaque = YES; // fix for strange issue in 4.x, need to set YES, set bg color image, then set back to NO
    cancelButton.backgroundColor = [UIColor colorWithPatternImage: cancelImage];
    cancelButton.opaque = NO;