如何让我的颜色渐变出现在我的自定义 UIView Swift

How can I get my color gradient to appear in my custom UIView Swift

我有一个自定义 UIView class 添加了 UIImageView 和 UILabel 子视图,但我无法在它们上面显示颜色渐变。

这是我的自定义 UIView class:

//
//  HomeTitleView.swift
//  Find My Lift
//
//  Created by Stephen Learmonth on 13/11/2020.
//  Copyright © 2020 Stephen Learmonth. All rights reserved.
//

import UIKit

class HomeTitleView: UIView {

    // MARK: - Properties
    
    private let homeTitleImageView: UIImageView = {
        let hiv = UIImageView()
        hiv.image = UIImage(named: "Stephen")
        hiv.contentMode = .scaleAspectFill
        hiv.clipsToBounds = true
        hiv.setHeight(height: 180)
        hiv.backgroundColor = .clear
        return hiv
    }()
    
    var homeTitleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 25)
        label.backgroundColor = .clear
        label.textColor = .white
        return label
    }()

    // MARK: - Lifecycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configureUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Helper Functions
    
    private func configureUI() {
        
        let gradient = CAGradientLayer()
        gradient.colors = [UIColor.clear.cgColor , UIColor.black.cgColor]
        gradient.locations = [0.85, 1]
        self.layer.addSublayer(gradient)
        gradient.frame = self.bounds
        
        self.addSubview(homeTitleImageView)
        homeTitleImageView.anchor(top: safeAreaLayoutGuide.topAnchor, left: leftAnchor,
                               bottom: bottomAnchor, right: rightAnchor,
                               paddingTop: 0, paddingLeft: 0,
                               paddingBottom: 0, paddingRight: 0)
        
        self.addSubview(homeTitleLabel)
        homeTitleLabel.anchor(left: leftAnchor, bottom: bottomAnchor,
                           paddingLeft: 12, paddingBottom: 30)
        
        


    }
}

我缺乏一些层和子视图的知识来理解它为什么不起作用。非常感谢收到任何帮助。

移动这条线

self.layer.addSublayer(gradient)

configureUI 结束,

override func layoutSubviews() {
  super.layoutSubviews()
  gradient.frame = self.bounds
}