gpt4 book ai didi

ios - 对于 Uiimageview 如何将 CGgradient 背景色设置为 ios swift 4 中的背景色?

转载 作者:行者123 更新时间:2023-11-30 11:49:23 25 4
gpt4 key购买 nike

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)

它不起作用...如何使用渐变图像背景颜色?

最佳答案

您可以从渐变中制作图像并将其设置在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)

如果你想在导航栏中设置。

self.navigationController?.navigationBar.setBackgroundImage(imageBG,
for: .default)

从渐变创建图像。

func gradientImage(fromLayer layer: CALayer) -> UIImage {
UIGraphicsBeginImageContext(layer.frame.size)

layer.render(in: UIGraphicsGetCurrentContext()!)

let outputImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return outputImage!
}

关于ios - 对于 Uiimageview 如何将 CGgradient 背景色设置为 ios swift 4 中的背景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495715/

25 4 0
文章推荐: ios - 我可以提供 SDWebImageCache 的 URL 以从缓存或下载返回图像吗?
文章推荐: java - 使用 OneToMany 属性上的过滤器查询实体会产生奇怪的结果
文章推荐: javascript - Jquery:如何根据计数克隆
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com