- navigationBar 设置背景图片的方法
self.navigationController?.navigationBar .setBackgroundImage(barImage, for: .default)
// 固定的图片会导致在不同机型上面变形所以我们需要自己创建uiimage对象并保存到本地
/// 创建APPBar 的背景图片 避免各机型不一致导致图片变形
/// - Returns: appbar 背景图片
static func creatNavigationBarGradientImageWithColors() -> UIImage {
let size = CGSize.init(width: KSCREENWIDHT, height: KNavigationHeight)
let colors = [UIColor.red,UIColor.yellow]
if colors.count == 0 || size == CGSize.zero {
return UIImage.init()
}
// 主要图层
let gradientLayer = CAGradientLayer.init()
gradientLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
gradientLayer.startPoint = CGPoint(x: 0, y: 0);
gradientLayer.endPoint = CGPoint(x: 1, y: 0);
var cgColors = [CGColor]()
colors.forEach { (color) in
cgColors.append(color.cgColor)
}
gradientLayer.colors = cgColors
// 其他小icon
let imageView = UIImageView.init(frame: CGRect(x: 100, y: size.height-44, width: 44, height: 44))
imageView.image = KHeadImage
gradientLayer.addSublayer(imageView.layer)
UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.isOpaque, 0);
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
guard let outPutImage = UIGraphicsGetImageFromCurrentImageContext() else {
UIGraphicsEndImageContext();
return UIImage()
};
UIGraphicsEndImageContext();
return outPutImage
}