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?
You can use the following snippet
view.layer.insertSublayer(yourLayer, atIndex: yourIndex)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With