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

问题描述 投票:0回答:2

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

enter image description here

ios xcode colors gradient
2个回答
0
投票

您希望Start PointX和End PointX为0.然后将Start PointY设置为0并将End PointY设置为1.这将从顶部到底部给出一个渐变,从顶部的顶部颜色和底部的底部颜色开始。


0
投票

我不知道你可以通过故事板添加渐变,说实话,但如果你通过代码这样做,你会有更多的选择。如更多颜色,颜色位置等

    // 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)
© www.soinside.com 2019 - 2024. All rights reserved.