swift - 快速在 View 和按钮上应用渐变颜色的问题

标签 swift colors gradient

我正在尝试在 View 和按钮上应用渐变颜色,但我遇到了一个问题,即它没有应用于整个按钮框架和 View ,正如您在屏幕截图中看到的那样。我已经使用了这段代码并给出了我自定义的十六进制颜色值,

func applyGradient(colours: [UIColor]) -> Void {
    let gradient: CAGradientLayer = CAGradientLayer()
    gradient.frame = self.bounds
    gradient.colors = colours.map { $0.cgColor }
    gradient.startPoint = CGPoint(x : 0.0, y : 0.5)
    gradient.endPoint = CGPoint(x :1.0, y: 1.5)
    self.layer.insertSublayer(gradient, at: 0)
}

并通过调用这个函数来应用,

self.headerView.applyGradient(colours: [UIColor.init(hexString: "54F3FF"),UIColor.init(hexString: "43D7FF") ,UIColor.init(hexString: "30B7FF")])

现在,当我运行应用程序并对其进行测试时,它看起来像这样 enter image description here

enter image description here

如何将渐变应用到全帧 View 和按钮?我的代码有什么错误?

最佳答案

试试这个

override func viewDidLayoutSubviews() {
        self.headerView.applyGradient(colours: [UIColor.init(hexString: "54F3FF"),UIColor.init(hexString: "43D7FF") ,UIColor.init(hexString: "30B7FF")])
    }

希望对你有帮助

关于swift - 快速在 View 和按钮上应用渐变颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787503/

相关文章:

java - 发送电子邮件时更改文本(字符串)的颜色

android - 如何在 Jetpack compose 中创建角度渐变?

css - 具有图像和线性渐变的多个背景的无效属性值

python - TensorFlow 如何计算 tf.train.GradientDescentOptimizer 的梯度?

ios - 在 Mac 上预先创建数组,将其添加到主包中以在 iOS 应用程序中访问

swift - RxSwift 重复 Action

swift - 如何在 Xcode 7 OSX 中创建 NSRadioButton 组

swift - 如何将 RxDataSource 与 SearchBar 一起使用?

android - BitmapFactory.decodeResource() 忽略 jpg 图像的 inPreferredConfig 选项

iphone - kCGColorSpaceGenericRGB 在 iPhone 上已弃用?