Gradient not covering full frame SWIFT - ios

I have the following extension to imageView:
extension UIImageView {
func addBlackGradientLayer(frame: CGRect){
let gradient = CAGradientLayer()
gradient.frame = frame
gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
gradient.locations = [0.0, 1.0]
self.layer.addSublayer(gradient)
}}
imageView.addBlackGradientLayer(frame: imageView.frame)
on applying this to the imageView, I am having the following output. I tried setting the constraints and also, the autolayout.
Following is the output after the execution:

You need to call
imageView.addBlackGradientLayer(frame: imageView.bounds)
See: Cocoa: What's the difference between the frame and the bounds?

Related

CAGradient layer not visible on view

I try to add a CAGradient effect in a pie chart I make with Core Graphics. It turns out I am not able and certainly not understand very well how to apply it to a view that is part of a CGPath...
override func draw(_ rect: CGRect)
{
...
func arc(myRadius: CGFloat, myStartAngle: CGFloat, myEndAngle: CGFloat) {
// This is the pie chart segment
context?.setFillColor(phenotypeColor.cgColor)
context?.move(to: center)
context?.addArc(center: center, radius: finalRadius, startAngle: myStartAngle, endAngle: myEndAngle, clockwise: true)
context?.fillPath()
(...)
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.colors = [UIColor.red, UIColor.blue]
self.layer.addSublayer(gradientLayer)
}
I don't see any gradient on the screen (only the white background...).
Lastly, I also tried to clip the context juste after saving its state but I am not sure I clip anything since I don't see any gradient (either on the whole view or just on the segment). I read the documentation but it does not clear things up (in my case...).
cgColor was missing... Sorry for the inconvenience.
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
self.layer.addSublayer(gradientLayer)

How to change background view color partialy in ios swift

New to swift i want my background view 80% height to be red and rest in blue. Currently i'm doing that adding two views. I think there is a direct way to do that. Any guidance or help will be appreciated.
Adding two views is a good way of getting what you want (and the best since you can set the % via constraints.)
An other way would be by adding sublayers to your view via the code:
let gradient: CAGradientLayer = CAGradientLayer()
let topColor = UIColor(red:223.0/255.0, green:142.0/255.0, blue:219.0/255.0, alpha:255.0/255.0).cgColor
let bottomColor = UIColor(red:0, green:201.0/255.0, blue:243.0/255.0, alpha:255.0*0.34).cgColor
gradient.colors = [topColor, UIColor.clear, bottomColor]
gradient.locations = [0.0 , 0.5, 1.0]
gradient.frame = self.view.bounds
self.view.layer.insertSublayer(gradient, at: 1)
I would only use the layer method if you want to do something funky like a gradient.
Thanks for help. I think i found a solution.Adding a sublayer does the trick.
self.view.backgroundColor = UIColor.red
let bottomBorder = CALayer()
let t = CGRect(x: 0.0, y: (self.view.frame.size.height/100.0)*80, width: self.view.frame.size.width, height: (self.view.frame.size.height/100.0)*20)
bottomBorder.frame = t
bottomBorder.backgroundColor = UIColor.blue.cgColor
self.view.layer.addSublayer(bottomBorder)

Swift PageViewController gradient background

I have a PageViewController and need to set a gradient background to it.
When using gradient background in normal ViewControllers, I use the following:
let layer = self.layer as! CAGradientLayer
layer.colors = [FirstColor.cgColor, SecondColor.cgColor]
layer.locations = [0.5]
when I try to use self.view.layer inside my PageViewController I get an error:
Could not cast value of type 'CALayer' (0x104f57900) to 'CAGradientLayer'
Can someone help me setting the background of a page view controller to an gradient?
Thanks
Hi you can create a UIView extension and just call like this:
view.applyGradient(colours: [UIColor.red, UIColor.green])
here the extension code
extension UIView {
func applyGradient(colours: [UIColor]) -> Void {
clipsToBounds = true
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
layer.insertSublayer(gradient, at: 0)
}
}
with this code you can apply to UIButtons, UILabels, anything that extends from UIView
hope it helps

How to add gradient to UIView as an extension

I am trying to add some gradient to a view in Xcode and for simplicity I tried to add my method as an extension to UIView:
extension UIView {
func applyGradient() {
let gradient = CAGradientLayer()
gradient.colors = [UIColor(hex: "F5FF8C").cgColor,
UIColor(hex: "F1F99D").cgColor,
UIColor(hex: "FDFFE0").cgColor]
gradient.locations = [0.0, 0.5, 1.0]
self.layer.insertSublayer(gradient, at: 0)
}
}
But apparently this doesn't work when I call it in my viewDidLoad:
self.myView.applyGradient()
Can someone point to me what am I doing wrong ?
In the question, you haven't set the frame at all. In the comment, your frame was not set correctly. This is the code that should work properly:
extension UIView {
func applyGradient() {
let gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor,
UIColor.green.cgColor,
UIColor.black.cgColor] // your colors go here
gradient.locations = [0.0, 0.5, 1.0]
gradient.frame = self.bounds
self.layer.insertSublayer(gradient, at: 0)
}
}
With your code:
With the modified code:
Explanation
gradient.frame.size = self.frame.size doesn't work, while gradient.frame = self.bounds does, because the frame attribute contains both the location and the size of the view, and even if you set the gradient's frame size, you do not specify the location of the gradient... So the gradient is never actually added to the view. By setting the frame attribute directly to the bounds of the view, you also add the location of the gradient in the view.

iOS fade to black at top of UIImageView

I have a UITableView where my cell backgroundView is a UIImageView. I would like top of each image to fade to black so I can overlay some white text.
I have looked at some answers like this, but none seem to be working.
In tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) I have tried:
var imageView = UIImageView(image: image)
var gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = imageView.frame
gradient.colors = [UIColor.blackColor(), UIColor.clearColor()]
gradient.locations = [0.0, 0.1]
imageView.layer.insertSublayer(gradient, atIndex: 0)
cell!.backgroundView = imageView
But I see no gradient and no difference from when I remove the gradient code. Is my gradient sitting under the image?
If I replace the line imageView.layer.insertSublayer(gradient, atIndex: 0) with imageView.layer.mask = gradient then my cells are just blank and white (no image anymore).
In your gradient.colors you need to have array of CGColor, so:
gradient.colors = [UIColor.blackColor().CGColor, UIColor.clearColor().CGColor]
I forget how the layers array is ordered. However, you should just add your gradient layer on top of your image view's layer using addSubLayer, not insertSublayer:atIndex: You want the gradient layer on top of the other layer so that it's non-opaque parts cover the image view.
For Swift 3.0, some slight tweaks required:
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = imageView.frame
gradient.colors = [UIColor.black.cgColor, UIColor.clear.cgColor]
gradient.locations = [0.0, 0.1]
imageView.layer.insertSublayer(gradient, at: 0)

Resources