[英]UIDocumentInteractionController doesn't respect UINavigationBar's barTintColor appearance in iOS 11
[英]Alpha isn't changing UINavigationBar's barTintColor
除了设置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!
}
}
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.