title: How to Apply Gradient to background view of iOS Swift App
body: <p>I'm trying to apply a gradient as the background color of a View (main view of a storyboard). The code runs, but nothing changes. I'm using xCode Beta 2 and Swift.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>class Colors {
    let colorTop = UIColor(red: 192.0/255.0, green: 38.0/255.0, blue: 42.0/255.0, alpha: 1.0)
    let colorBottom = UIColor(red: 35.0/255.0, green: 2.0/255.0, blue: 2.0/255.0, alpha: 1.0)
  
    let gl: CAGradientLayer
  
    init() {
      gl = CAGradientLayer()
      gl.colors = [ colorTop, colorBottom]
      gl.locations = [ 0.0, 1.0]
    }
  }
  </code></pre>
  
  <p>then in the view controller:</p>
  
  <pre><code>  let colors = Colors()
  
    func refresh() {
          view.backgroundColor = UIColor.clearColor()
          var backgroundLayer = colors.gl
          backgroundLayer.frame = view.frame
          view.layer.insertSublayer(backgroundLayer, atIndex: 0)
        }
      }
    }
  </code></pre>