在 Interface Builder 中为渐变视图设置渐变背景颜色

Setting up Gradient Background Colors in Interface Builder for a Gradient View

我在属性检查器中启用了渐变视图,但我无法修复颜色以显示更多颜色过渡,还显示颜色从下到上而不是从左到右?右侧窗格中需要进行哪些修改?

您希望 Start PointX 和 End PointX 为 0。然后将 Start PointY 设置为 0,将 End PointY 设置为 1。这将提供从上到下的渐变,顶部颜色为顶部,底部颜色为底部。

老实说,我不知道您可以通过故事板添加渐变,但如果您通过代码执行此操作,您将有更多选择。比如更多颜色,颜色位置等

    // Create a gradient layer
   let gradient: CAGradientLayer = CAGradientLayer()
    // Create an array of colours you can use, as many colours as you require
    gradient.colors = [.blue.cgColor, .red.cgColor, .orange.cgColor].cgColor
    // Chose the locations for your colors, 0.5 is center
    gradient.locations = [0.0, 0.5, 1.0]
    // Start and end points so this goes from y: 0.0 to y: 1.0 so top to bottom
    gradient.startPoint = CGPoint(x: 1.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
    // Set the frame
    gradient.frame = CGRect(x: 0.0, y: 0.0, width: yourView.frame.size.width, height: yourView.frame.size.height)
    // Add to view
    yourView.layer.insertSublayer(gradient, at: 0)