ios - 快速将渐变层推送到背景

标签 ios swift background swift2

我的应用程序有一个我用代码制作的渐变层。然而,当我在屏幕上显示它时,它覆盖了所有其他组件并隐藏了我的按钮、标签和图像。

如何将此图层推回以使其成为 View 的背景?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //creating color gradient
    let topColor = UIColor(red: 69/255, green: 140/255, blue: 68/255, alpha: 1).CGColor
    let bottomColor = UIColor(red: 143/255, green: 239/255, blue: 141/255, alpha: 1).CGColor
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = self.view.frame
    gradientLayer.colors = [topColor,bottomColor]
    gradientLayer.locations = [0.1,1.0]
    self.view.layer.addSublayer(gradientLayer)   
}

最佳答案

您可以在特定索引处插入子图层。

self.view.layer.insertSublayer(gradientLayer, at: 0)

关于ios - 快速将渐变层推送到背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41851067/

相关文章:

ios - Objective-C:如何检测发件人是 UITextField 还是 UISwitch

ios - 初始化程序和函数中的参数之间的 Swift 语法差异

ios - 我如何在 UITableViewCell 或 UICollectionViewCell 中编写仅影响其所在单元格的 UIButton 扩展?

ios - 当应用程序处于后台时,核心蓝牙框架不会向 iOS 应用程序发送数据

ios - iOS 是否允许将用户重定向到网站进行付款?

ios - React Native 模块,未找到 Cocoapod 文件

SwiftUI View 阻止对 MapKit 的触摸,但不会阻止其他 View

ios - 如何处理 NSData contentsOfURL EXC_BREAKPOINT 错误

Android ImageButton 背景色

java - 如何设置JPanel的透明背景?