Ios 阿尔法是';t改变UINavigationBar';巴丁特色
我想用alpha 0.6将UINavigationBar的颜色设置为黑色,以查看我的界面。但如果我使用此代码:Ios 阿尔法是';t改变UINavigationBar';巴丁特色,ios,xcode,swift,uinavigationbar,Ios,Xcode,Swift,Uinavigationbar,我想用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,它看起来仍然
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,它看起来仍然像:
我怎么能让它透明的黑色透明颜色
感谢您,您不必设置BartinColor,而必须将navgationBar的背景图像设置为相同的颜色。请参见此处的类似问题: 但是你必须得到这种特殊颜色的图像。你可以把这个图像放到你的项目中,然后使用
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)
这将起作用。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!
}
}
由于较低的iOS版本存在一些问题,您必须从颜色获取图像。请使用此方法将UIColor转换为背景图像。
Xmarin.iOS
调用此方法
var bgImage=GetImageFromColor(UIColor.FromRGBA(r,g,b,alpha),新的CGSize(viewWidth,viewHeight)) 好的,我添加了这个:self.navigationController?.navigationBar.setBackgroundImage(UIImage(),forBarMetrics:UIBarMetrics.Default),它很清楚,但我希望这里有黑色的alpha 0.6问题是指本机iOS开发,而不是Xamarin。
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;
}