[英]For Uiimageview how to set the CGgradient background colour as background colour in ios swift4?
var navigationImageView:UIImageView! = UIImageView()
navigationImageView.frame = (0,0,320,64)
var gradient = CAGradientLayer()
let gradientDark = UIColor(red: 148/255.0, green:210/255.0, blue: 245/255.0, alpha: 1.0)
let gradientLight = UIColor(red: 222/255.0, green:247/255.0, blue: 230/255.0, alpha: 1.0)
let gradientLight1 = UIColor(red: 245/255.0, green:247/255.0, blue: 206/255.0, alpha: 1.0)
gradient.frame = navigationImageView.bounds
let color1 = gradientDark.cgColor
let color2 = gradientLight.cgColor
let color3 = gradientLight1.cgColor
gradient.colors = [color1, color2, color3]
gradient.colors = [gradientDark.cgColor,gradientLight.cgColor,gradientLight1.cgColor];
navigationImageView.layer.insertSublayer(gradient, at: 0)
Its not working... How to se the cgradient image backgorund colour? 它不起作用...如何确定渐变图像的backgorund颜色?
You can make image from gradient and set it in imageView
您可以从渐变制作图像并在imageView
设置
let gradient = CAGradientLayer()
let screenWidth = UIScreen.main.bounds.size.width
let defaultNavigationBarFrame = CGRect(x: 0, y: 0, width: screenWidth, height: 64)
gradient.frame = defaultNavigationBarFrame
//colors
let gradientDark = UIColor(red: 148/255.0, green:210/255.0, blue: 245/255.0, alpha: 1.0)
let gradientLight = UIColor(red: 222/255.0, green:247/255.0, blue: 230/255.0, alpha: 1.0)
let gradientLight1 = UIColor(red: 245/255.0, green:247/255.0, blue: 206/255.0, alpha: 1.0)
gradient.colors = [gradientDark.cgColor,gradientLight.cgColor,gradientLight1.cgColor]
// Create image.
let imageBG: UIImage = self.gradientImage(fromLayer: gradient)
If you want to set it in navigation bar. 如果要在导航栏中进行设置。
self.navigationController?.navigationBar.setBackgroundImage(imageBG,
for: .default)
Create image from gradient. 从渐变创建图像。
func gradientImage(fromLayer layer: CALayer) -> UIImage {
UIGraphicsBeginImageContext(layer.frame.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.