我想用alpha 0.6将UINavigationBar的颜色设置为黑色,以查看我的界面。但是如果我使用这个代码:
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类似:

我怎么能让它透明的黑色透明的颜色?
谢谢
发布于 2015-09-12 02:11:28
不必设置barTintColor,您必须用相同的color.See设置navgationBar的背景图像,如下所示:
I can't set UINavigationBar's barTintColor to clearColor successfully
但是您必须获得这个特殊的color.You的图像,可以将这个图像放到您的项目中,然后使用
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;
}并在视图加载中调用这个函数:
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)这会管用的。
发布于 2017-03-23 17:09:59
Swift 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!
}
}发布于 2017-11-16 13:20:58
您必须从颜色获取图像,因为低iOS version.Use存在一些问题,这种方法可以将您的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),新CGSize(viewWidth,viewHeight));
https://stackoverflow.com/questions/32528163
复制相似问题