Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a sublayer in swift?

Tags:

swift

I am creating a view and adding a gradient layer to it.

I have this:

import UIKit
import QuartzCore


let rect : CGRect = CGRectMake(0,0,320,100)

var vista : UIView = init(frame: rect)

let gradient : CAGradientLayer = CAGradientLayer()
gradient.frame = vista.bounds

let cor1 = UIColor.blackColor().CGColor
let cor2 = UIColor.whiteColor().CGColor

let arrayColors = [cor1, cor2]

gradient.colors = arrayColors

now I have to do this

[view.layer insertSublayer:gradient atIndex:0];

How do I do that in swift?

like image 406
Duck Avatar asked Jun 08 '14 22:06

Duck


2 Answers

You can use the following snippet

view.layer.insertSublayer(yourLayer, atIndex: yourIndex)
like image 58
Omar Abdelhafith Avatar answered Nov 04 '22 08:11

Omar Abdelhafith


Have you tried this?

view.layer.insertSublayer(gradient, atIndex:0)

Note that whenever you're editing Swift code, you can command-click on a symbol from the frameworks to see a Swift-ified version of that API's header file. Also, the documentation online and in Xcode shows Swift syntax for all the APIs it covers.

like image 35
rickster Avatar answered Nov 04 '22 08:11

rickster