Last active
August 29, 2015 14:24
-
-
Save mingsai/c73f7f5a0a3a91120633 to your computer and use it in GitHub Desktop.
A swift extension that allows one to use gradient colors on all UIControls and UIViews and subviews (e.g. UIButton, etc. pretty much anything on screen)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MNGLayerExtensions.swift | |
| // | |
| // | |
| // Created by Tommie N. Carter, Jr., MBA on 6/21/15. | |
| // Copyright © 2015 MING Technology. All rights reserved. | |
| // | |
| // Sample usage: self.view.layer.configureGradientBackground(UIColor.purpleColor().CGColor, UIColor.blueColor().CGColor, UIColor.whiteColor().CGColor) | |
| // Nota Bene: Function can be applied to any UIControl that also has a view.layer property. Resolves the issue of gradient not covering full area on rotation by resizing the layer to a square | |
| import QuartzCore | |
| extension CALayer { | |
| //creates a gradient background with varidac arguments | |
| func configureGradientBackground(colors:CGColorRef...){ | |
| let gradient = CAGradientLayer() | |
| let maxWidth = max(self.bounds.size.height,self.bounds.size.width) | |
| let squareFrame = CGRect(origin: self.bounds.origin, size: CGSizeMake(maxWidth, maxWidth)) | |
| gradient.frame = squareFrame | |
| gradient.colors = colors | |
| self.insertSublayer(gradient, atIndex: 0) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment