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,您还必须使用相同的 color.See 此处类似问题设置导航栏的背景图像:
I can't set UINavigationBar's barTintColor to clearColor successfully
但是你必须得到这个特别的图像 color.You 可以把这个图像放到你的项目中,然后使用
let image = UIImage(named: "yourimage'name")
获取图像。
或者您可以向您的视图控制器添加一个函数:
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)
这会起作用。
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!
}
}
您必须从 Color 获取图像,因为较低 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), new CGSize(viewWidth, viewHeight));
我想将 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,您还必须使用相同的 color.See 此处类似问题设置导航栏的背景图像:
I can't set UINavigationBar's barTintColor to clearColor successfully
但是你必须得到这个特别的图像 color.You 可以把这个图像放到你的项目中,然后使用
let image = UIImage(named: "yourimage'name")
获取图像。
或者您可以向您的视图控制器添加一个函数:
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)
这会起作用。
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!
}
}
您必须从 Color 获取图像,因为较低 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), new CGSize(viewWidth, viewHeight));