Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 CAGradientLayer不更改背景色_Ios_Swift_Uiview - Fatal编程技术网

Ios CAGradientLayer不更改背景色

Ios CAGradientLayer不更改背景色,ios,swift,uiview,Ios,Swift,Uiview,我想为我的UIView创建一个渐变作为背景色。我写了这段代码 gradient.colors = [UIColor.blue,UIColor.yellow] gradient.locations = [0.0 , 1.0] gradient.startPoint = CGPoint(x: 0.0, y: 1.0) gradient.endPoint = CGPoint(x: 1.0, y: 1.0) gradient.frame =

我想为我的UIView创建一个渐变作为背景色。我写了这段代码

gradient.colors = [UIColor.blue,UIColor.yellow]
        gradient.locations = [0.0 , 1.0]
        gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
        gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)


self.view.layer.insertSublayer(gradient, at: 0)
但是视图在界面构建上仍然是相同的。如果我在代码之前添加这一行

self.view.backgroundColor = UIColor.clear
背景色变为白色

        let gradient: CAGradientLayer = CAGradientLayer()

        gradient.colors = [UIColor.blue.cgColor, UIColor.yellow.cgColor]
        gradient.locations = [0.0 , 1.0]
        gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
        gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)

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

试试这个代码。swift 3对我有效好的,很高兴它解决了你的问题:)你也能投票给我的答案吗?@cole-为了将来的参考,当你发布答案时,试着包括为什么-而不仅仅是“这里,这个代码有效”。好的,谢谢你的反馈。下次我会记住的