繁体   English   中英

Alpha不会更改UINavigationBar的barTintColor

[英]Alpha isn't changing UINavigationBar's barTintColor

我想将UINavigationBar的颜色设置为带有alpha 0.6的黑色以查看我的界面。 但是,如果我使用此代码:

self.navigationController?.navigationBar.barTintColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.6)
self.navigationController?.navigationBar.translucent = true

仍然看起来像alpha 1.0:

在此处输入图片说明

如何使它具有黑色透明色透明?

谢谢

除了设置barTintColor之外,还必须将navgationBar的背景图像设置为相同的颜色。

我无法成功将UINavigationBar的barTintColor设置为clearColor

但是您必须获得具有这种独特颜色的图像。您可以将该图像放入项目中,然后使用

        let image = UIImage(named: "yourimage'name")

获得图像。

或者您可以将func添加到视图控制器中:

 func imageFromColor(color:UIColor,size:CGSize)->UIImage{
    let rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    let context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);

    var image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContext(size)
    image.drawInRect(rect)
    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image;
}

并在viewdidload中将此函数称为:

    let color=UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
    let image=imageFromColor(color, size: CGSizeMake(1, 1))

    self.navigationController?.navigationBar.translucent=true;
    self.navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default)

这将起作用。

斯威夫特3:

func imageFromColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()

    context!.setFillColor(color.cgColor)
    context!.fill(rect)

    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIGraphicsBeginImageContext(size)
    image?.draw(in: rect)
    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}

并作为UIColor扩展

extension UIColor {
    func toImage(withSize size: CGSize) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        context!.setFillColor(self.cgColor)
        context!.fill(rect)

        var image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        UIGraphicsBeginImageContext(size)
        image?.draw(in: rect)
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }
}

您必须从Color获取图像,因为较低的iOS版本存在一些问题。使用此方法可以将UIColor转换为背景图像。

Xmarin.iOS

private static UIImage GetImageFromColor(UIColor color, CGSize size)
            {
                var rect = new CGRect(0, 0, size.Width, size.Height);
                UIGraphics.BeginImageContextWithOptions(size, false, 0);
                color.SetFill();
                UIGraphics.RectFill(rect);
                var image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                return image;
            }

调用此方法

var bgImage = GetImageFromColor(UIColor.FromRGBA(r,g,b,alpha),new CGSize(viewWidth,viewHeight));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM