title: Gradient Layer of CollectionViewCell's subview exists only on first admission of View COntroller
body: <p>I have a <code>CollectionViewViewController</code> which is pushed from another one. This <code>CollectionViewViewController</code>'s view has two subviews: simple UIView with label inside and <code>UICollectionView</code>. When it's presented for the first time everything works totally fine, and gradient is being shown correctly. But when I pop this View Controller (so it's being deallocated) and push it again from the same parent View Controlle, the gradient isn't being shown.</p>
  
  <p>The cell is dequeued like below:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageScrollGeminiCellIdentifier, for: indexPath) as! PageScrollGeminiCell
      let item = viewModel.items[indexPath.item]
      cell.configure(withTitle: item.name, description: item. description, imageURL: item.imageUrl, gradientColor: item.color, theme: theme)
      mainView.geminiView.animateCell(cell)
      return cell
  }
  </code></pre>
  
  <p>And here is what I do in cell's class method and cell life cycle:</p>
  
  <pre><code>private var gradientColor: UIColor?
  private var gradientLayer: CAGradientLayer?
  [...]
  override init(frame: CGRect) {
      super.init(frame: .zero)
      setupView()
  }
  required init?(coder aDecoder: NSCoder) {
      return nil
  }
  override func layoutSubviews() {
      if gradientLayer != nil {
          gradientView.layer.sublayers = nil
      }
      if let color = gradientColor {
          let gradientPoints = (CGPoint(x: 0.0, y: 0.15), CGPoint(x: 0.0, y: 1.0))
          self.gradientLayer = gradientView.applyGradient(with: [color, color.withAlphaComponent(0.08)], gradient: .vertical(gradientPoints))
      }
  }
  // MARK: Private
  private func setupView() {
      clipsToBounds = true
      layer.cornerRadius = 15
      backgroundColor = .clear
      addSubview(containerView)
      containerView.addSubview(backgroundImageView)
      containerView.addSubview(gradientView)
      containerView.addSubview(labelsContainerView)
      labelsContainerView.addSubview(titleLabel)
      labelsContainerView.addSubview(descriptionLabel)
      containerView.snp.makeConstraints { maker in
          maker.edges.equalToSuperview()
      }
      backgroundImageView.snp.makeConstraints { maker in
          maker.edges.equalToSuperview()
      }
      gradientView.snp.makeConstraints { maker in
          maker.leading.trailing.top.bottom.equalToSuperview()
      }
      labelsContainerView.snp.makeConstraints { maker in
          maker.leading.top.equalTo(16)
          maker.trailing.equalTo(-16)
      }
      titleLabel.snp.makeConstraints { maker in
          maker.top.leading.trailing.equalToSuperview()
      }
      descriptionLabel.snp.makeConstraints { maker in
          maker.top.equalTo(titleLabel.snp.bottom).offset(8)
          maker.leading.trailing.bottom.equalToSuperview()
      }
  }
  // MARK: - Internal
  func configure(withTitle title: String?, description: String?, imageURL: String?, gradientColor: UIColor?, theme: Themeable) {
      backgroundImageView.setImage(withString: imageURL)
      titleLabel.text = title
      titleLabel.font = theme.font.h2
      descriptionLabel.text = description
      descriptionLabel.font = theme.font.b1
      self.gradientColor = gradientColor
  }
  </code></pre>
  
  <p>Also I want to show you <code>applyGradient</code> func:</p>
  
  <pre><code>func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) -&gt; CAGradientLayer {
      layoutSubviews()
      let gradient = CAGradientLayer()
      gradient.frame = self.bounds
      gradient.colors = colours.map { $0.cgColor }
      gradient.startPoint = orientation.startPoint
      gradient.endPoint = orientation.endPoint
      gradient.cornerRadius = layer.cornerRadius
      self.layer.insertSublayer(gradient, at: 0)
      return gradient
  }
  </code></pre>
  
  <p>What I discovered is that when the View Controller is being presented for the first time layoutSubviews() method is being called two times, but every next time is being called only one time per each cell.</p>
  
  <p>For now I'm not quiet sure if I'm not inserting more than 1 sublayer to my cell, but I'm sure that I'm clearing sublayer array, so I think it's not the problem.</p>