Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 水平渐变颜色在Swift中仅显示一半?_Ios_Swift_Colors_Gradient - Fatal编程技术网

Ios 水平渐变颜色在Swift中仅显示一半?

Ios 水平渐变颜色在Swift中仅显示一半?,ios,swift,colors,gradient,Ios,Swift,Colors,Gradient,我正在尝试为按钮Baground提供渐变色,我正在尝试以下代码 func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) { let gradientLayer = CAGradientLayer() gradientLayer.frame = bounds gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]

我正在尝试为按钮Baground提供渐变色,我正在尝试以下代码

func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
        layer.insertSublayer(gradientLayer, at: 0)
    }

@IBOutlet weak var loginbtn: UIButton!
loginbtn.setGradientBackground(colorOne: UIColor.red, colorTwo: UIColor.blue)
override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        loginbtn.setGradientBackground(colorOne: UIColor.red, colorTwo: UIColor.green)
    }
我正在尝试访问这个方法,它将显示这样的输出,如何解决这个问题

如果我添加以下代码

func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
        layer.insertSublayer(gradientLayer, at: 0)
    }

@IBOutlet weak var loginbtn: UIButton!
loginbtn.setGradientBackground(colorOne: UIColor.red, colorTwo: UIColor.blue)
override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        loginbtn.setGradientBackground(colorOne: UIColor.red, colorTwo: UIColor.green)
    }
它将显示以下错误。

确保在自动布局设置按钮大小后设置渐变帧

如果对按钮进行子类化,则可以在
layoutSubviews()
中执行此操作

否则,在视图控制器中执行此操作的好位置是
viewdilayoutsubviews()


编辑

基于您发布的第二幅图像-从
viewdilayoutsubviews()
调用时获得重叠渐变。。。由于auto layout“发挥作用”,因此可以多次调用,并且每次调用时都会添加一个新层

我不知道您在自定义类中可能还做了什么,但请尝试这样做:

class GradButton: UIButton {

    let gradientLayer = CAGradientLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() -> Void {

        gradientLayer.locations = [0.0, 1.0]

        // top-right to bottom-left
        gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)

        // we only add the layer once, here when called from init    
        layer.addSublayer(gradientLayer)

    }

    func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
    }

}

class MyTest: UIViewController {

    @IBOutlet var loginbtn: GradButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        loginbtn.setGradientBackground(colorOne: .red, colorTwo: .yellow)
    }

}
结果:


您的代码正在运行,但在结尾部分,它显示为jointSee: