HomeiOS Developmentios - Gradient view does not cowl the entire width of the...

ios – Gradient view does not cowl the entire width of the gathering view Swift


I am making an attempt to use a gradient to a view that may be a constraint to the highest, left, and proper of the gathering view however for some purpose, the gradient does not cowl the entire width of the view that’s utilized to (see the pink within the image).

View Controller Code:-

extension HomeView : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
    1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
    cell.storyImage.picture = UIImage(named: storyImages[indexPath.item].mediaURL ?? EMPTY_STR)
    cell.gradientView.body = cell.storyImage.body
    return cell
 }

func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.body.width, top: collectionView.body.top)
  }
}

Cell Code:-

import UIKit

class CollectionCell: UICollectionViewCell {

@IBOutlet weak var sImage: UIImageView!
@IBOutlet weak var gradientView: GradientView!

override func layoutSubviews() {
    tremendous.layoutSubviews()
  }
}

class GradientView: UIView {

override func awakeFromNib() {
    tremendous.awakeFromNib()
    let gradientLayer = CAGradientLayer()
    gradientLayer.body = CGRect(x: 0, y: 0, width: body.width, top: body.top)
    gradientLayer.colours = [UIColor.white.cgColor,UIColor.red.withAlphaComponent(0.5).cgColor]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
    layer.addSublayer(gradientLayer)
   }
}

Output:-

ScreenShot

Principal Storyboard

ScreenShot

Query: How one can set the width in line with the gathering view width?

Can somebody please clarify to me how to do that, I’ve tried with the above code however don’t have any outcomes but. Please Appropriate me if I am doing flawed.

Any assist can be vastly appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments