【问题标题】:Change the bar tint color makes the blurry area becomes black and white更改条形色调颜色使模糊区域变为黑白
【发布时间】:2014-02-14 00:52:45
【问题描述】:

我想在我的视图上添加一个模糊效果,我使用了UIToolbar 来做到这一点。但是当我尝试在UIToolbar 上添加条形颜色时,发生了一些奇怪的事情。

这是我用来添加UIToolbar的代码,很基础,没什么花哨的:

 UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.blurView.frame.size.width, self.blurView.frame.size.height)];
[self.blurView addSubview:toolbar];

在我添加 barTintColor 之前,一切看起来都不错:

但是在我添加了条形颜色之后:

[toolbar setBarTintColor:[UIColor colorWithWhite:0.000 alpha:0.500]];

工具栏下的一切都变成了黑白:

一开始我以为颜色或者透明度有问题,我一直在玩不同的颜色和不同的透明度。只要我使用setBarTintColor,它就会变成那样的黑白。例如,随机的蓝绿色:

【问题讨论】:

    标签: ios ios7 uitoolbar blur


    【解决方案1】:

    这似乎是 UIToolbar 的问题。

    您可以添加一个具有小于 1 alpha 分量的背景颜色的 CALayer,而不是设置色调颜色,具体取决于您希望颜色的突出程度,如下所示:

        CALayer *extraColorLayer = [CALayer layer];
        extraColorLayer.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
        extraColorLayer.backgroundColor = [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:0.5].CGColor;
        [toolbar.layer addSublayer:extraColorLayer];
    

    这似乎创造了您正在寻找的效果。

    改编自this answer

    【讨论】:

      【解决方案2】:

      我认为这种情况对于黑白情况来说更好:

      • iOS 7.0

        UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:self.blurView.bounds];
        if (isBlack)
            toolbar.barStyle = UIBarStyleBlackTranslucent;
        else
            toolbar.barStyle = UIBarStyleDefault;
        toolbar.autoresizingMask = self.blurView.autoresizingMask;
        [self.blurView insertSubview:blurEffect atIndex:0];
        
      • iOS 8.0 或更高版本

        UIBlurEffect *effect;
        if (isBlack)
            effect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        else
            effect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        UIVisualEffectView *blurEffect = [[UIVisualEffectView alloc] initWithEffect:effect];
        [self.blurView insertSubview:blurEffect atIndex:0];
        

      您可以创建一个继承自 UIView 的类,并关联到您需要在 Storyboard 上模糊的视图(在自定义类上)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 2012-01-03
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多