首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阿尔法没有改变UINavigationBar的barTintColor

阿尔法没有改变UINavigationBar的barTintColor
EN

Stack Overflow用户
提问于 2015-09-11 16:33:27
回答 3查看 1.6K关注 0票数 2

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

代码语言:javascript
复制
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类似:

我怎么能让它透明的黑色透明的颜色?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-12 02:11:28

不必设置barTintColor,您必须用相同的color.See设置navgationBar的背景图像,如下所示:

I can't set UINavigationBar's barTintColor to clearColor successfully

但是您必须获得这个特殊的color.You的图像,可以将这个图像放到您的项目中,然后使用

代码语言:javascript
复制
        let image = UIImage(named: "yourimage'name")

才能得到图像。

或者,您可以向视图控制器添加一个func:

代码语言:javascript
复制
 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;
}

并在视图加载中调用这个函数:

代码语言:javascript
复制
    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)

这会管用的。

票数 1
EN

Stack Overflow用户

发布于 2017-03-23 17:09:59

Swift 3:

代码语言:javascript
复制
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扩展

代码语言:javascript
复制
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!
    }
}
票数 0
EN

Stack Overflow用户

发布于 2017-11-16 13:20:58

您必须从颜色获取图像,因为低iOS version.Use存在一些问题,这种方法可以将您的UIColor转换为背景图像。

Xmarin.iOS

代码语言:javascript
复制
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),新CGSize(viewWidth,viewHeight));

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32528163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档