How To Add A Gradient Background In Swift

If you try and add a gradient background in Swift to a View  you will more than likely get this error: fatal error: array element cannot be bridged to Objective-C
I hit this problem trying to add a gradient background. My initial implementation of a gradient background looked like this:

How to successfully add a gradient background in Swift

Extensions add new functionality to an existing class, structure of enumeration type. I opted to extend CAGradientLayer with a new function turquoiseColor() allowing me to get a new torquiseColor gradient directly from the CAGradientLayer class. The code below shows how to display the gradient as a background for a view.

This code should of worked, unfortunately it didn’t :-[. When I ran the application I got the error: “fatal error: array element cannot be bridged to Objective-C”. The stackoverflow post provided the solution; gradient.colors = arrayColors calls an Objective-C method thats expects an NSArray.

When you bridge from a Swift array to an NSArray object, the elements in the Swift array must be AnyObject compatible. For example, a Swift array of type Int[] contains Int structure elements. The Int type is not an instance of a class, but because the Int type bridges to the NSNumber class, the Int type is AnyObject compatible. Therefore, you can bridge a Swift array of type Int[] to an NSArray object. If an element in a Swift array is not AnyObject compatible, a runtime error occurs when you bridge to an NSArray object.

You can also create an NSArray object directly from a Swift array literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSArray object and assign it an array literal, Swift creates an NSArray object instead of a Swift array.

Declaring the array as Array <AnyObject> overcomes this issue though:

Your gradient will now be successfully displayed. As you need more gradients of different colors, these colors can bee added to the the CAGradientLayer extension.

9 replies
  1. Adam
    Adam says:

    hi, i am trying to use this gradient on a GameScene background. can’t figure out how to get this gradient BEHIND all the other sprites on the view ??

    cheers

    Reply
  2. Taylor
    Taylor says:

    I am trying to use this to get a gradient background color view on a NSView in a OS X application but whenever I go to run it I get “fatal error: unexpectedly found nil while unwrapping an Optional value
    (lldb) “

    Reply
    • developerdave
      developerdave says:

      For some reason the variable you are unwrapping is nil. Have you got a sample of code you can share? I can see if I can spot why for you – not done much OS X programming though.

      Reply

Trackbacks & Pingbacks

  1. […] wrote an article a while back: How To Add A Gradient Background In Swift. I got some comments asking some further details on how to add using XCode. I published the code up […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *