Programmatically Create a Uiview With Color Gradient

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 calling setGradientBackground() on viewDidAppear:

    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:

Sample Image

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



Leave a reply



Submit