Programmatically create a UIView with color gradient
Objective-C:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor blackColor].CGColor];
[view.layer insertSublayer:gradient atIndex:0];
Swift:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.white.cgColor, UIColor.black.cgColor]
view.layer.insertSublayer(gradient, at: 0)
Info: use startPoint and endPoint to change direction of gradient.
If there are any other views added onto this UIView
(such as a UILabel
), you may want to consider setting the background color of those UIView
’s to [UIColor clearColor]
so the gradient view is presented instead of the background color for sub views. Using clearColor
has a slight performance hit.
How to make a UIView gradient effect using 3 colors
Just add:
gradientLayer.colors = [topColor.cgColor, ,bottomColor.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0] //you can remove this line if you want even gradient.
gradientLayer.colors
and gradientLayer.locations
are not limited to just two inputs each.
Also, as far as startPoint
and endPoint
default to (0.5,0.0) and (0.5,1.0) respectively. You can refer to what this would look like here:
http://ios-tutorial.com/create-uiview-gradient-background/
Swift: Background gradient for UIView
There were 2 problems:
I needed to set the start and end points to provide a gradient direction:
func setGradientBackground(colorTop: UIColor, colorBottom: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorBottom.cgColor, colorTop.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
gradientLayer.locations = [0, 1]
gradientLayer.frame = bounds
layer.insertSublayer(gradientLayer, at: 0)
}And the second issue was that
CAGradientLayer
takes effect after the view is layed out. I solved that callingsetGradientBackground()
onviewDidAppear
:override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
separatorView.setGradientBackground(colorTop: .clear, colorBottom: Colors.darkGrey)
}
How can I get my color gradient to appear in my custom UIView Swift
Move this line
self.layer.addSublayer(gradient)
end of configureUI
, and
override func layoutSubviews() {
super.layoutSubviews()
gradient.frame = self.bounds
}
Adding Gradient View and UILabel Programmatically to a UIView Is Not Working
May be you have any issues with constraints. I've checked your code (I changed setGradientBackground(colorTop: .darkBlue, colorBottom: .systemIndigo, view: notAvailableView)
to setGradientBackground(colorTop: .darkGray, colorBottom: .systemIndigo, view: notAvailableView)
because I haven't this color and I see normal behavior:
Add gradient color to a custom UIView
Set the gradientColor
layer's mask
property to CAShapeLayer()
that has the correct shape filled with black
let mask = CAShapeLayer()
mask.frame = // to whatever size it should be
mask.path = yourShapeBezierHERE.cgPath
mask.fillColor = UIColor.black.cgColor
gradientColor.mask = mask
How to Apply Gradient to background view of iOS Swift App
The Colors you're providing to gradient must be of type CGColor
. So set your array of CGColor
to gl.colors
.
The correct code is :
class Colors {
var gl:CAGradientLayer!
init() {
let colorTop = UIColor(red: 192.0 / 255.0, green: 38.0 / 255.0, blue: 42.0 / 255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 35.0 / 255.0, green: 2.0 / 255.0, blue: 2.0 / 255.0, alpha: 1.0).cgColor
self.gl = CAGradientLayer()
self.gl.colors = [colorTop, colorBottom]
self.gl.locations = [0.0, 1.0]
}
}
Related Topics
Cordova App Hanging During Startup on iOS 10
Create Uiimage With Solid Color in Swift
Using Ssl in an Iphone App - Export Compliance
How to Share an Image on Instagram in Ios
Add Placeholder Text Inside Uitextview in Swift
Add Image to Uialertaction in Uialertcontroller
How to Modify Swift_Module_Name
How to Parse Json Response from Alamofire API in Swift
Ios App With Arc, Find Who Is Owner of an Object
How to Get the Day of the Week With Foundation
How to Create Ns_Options-Style Bitmask Enumerations in Swift
Uisplitviewcontroller in Portrait on Iphone Shows Detail Vc Instead of Master
How to Use Auto Layout to Move Other Views When a View Is Hidden
How to Determine the Os Version At Runtime in Os X or iOS (Without Using Gestalt)