透明度(alpha)在swift中从第二次运行中删除

时间:2015-07-06 08:35:54

标签: swift transparency uicolor

我正在通过以下代码加载子视图:

HideView.backgroundColor = UIColor.clearColor()
var loadingView = UIView (frame: CGRectMake(120, 190, 90, 90));
loadingView.center = HideView.center
loadingView.backgroundColor = UIColor(red:0.76, green:0.76, blue:0.76, alpha:0.9)
loadingView.clipsToBounds = true;
loadingView.layer.cornerRadius = 10.0
loadingView.layer.shouldRasterize = true
HideView.addSubview(loadingView)

上面的代码工作正常,我们可以看到如下背景:

enter image description here

但是当再次出现此屏幕子视图时(从第二次开始),透明度将消失如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在添加“loadingView”' ' HiddenView'两次。这就是它在第二轮看起来被阻挡的原因。

相反,请确保只添加一次。

HideView.backgroundColor = UIColor.clearColor()
if (!addedLoadingView) { // Or anything alike...
    addedLoadingView = YES
    var loadingView = UIView (frame: CGRectMake(120, 190, 90, 90));
    loadingView.center = HideView.center
    loadingView.backgroundColor = UIColor(red:0.76, green:0.76, blue:0.76, alpha:0.9)
    loadingView.clipsToBounds = true;
    loadingView.layer.cornerRadius = 10.0
    loadingView.layer.shouldRasterize = true
    HideView.addSubview(loadingView)
}