在UIView上应用渐变无效

7
我正在尝试以编程方式向UIView添加渐变,但它没有起作用。它似乎根本没有颜色。我附上了相关的代码和屏幕截图。请注意,我正在应用渐变的底部正方形。有人可以帮助我找出我在这里做错了什么吗?
let sundayView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()
    setupViews()
    setupSundayView()
}

func setupViews() {
    sundayView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(sundayView)
}

func setupSundayView() {
    sundayView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activateConstraints([
        sundayView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor),
        sundayView.topAnchor.constraintEqualToAnchor(fridayView.bottomAnchor, constant: 16.0),
        sundayView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -8.0),            
        sundayView.heightAnchor.constraintEqualToAnchor(mondayView.heightAnchor),
        sundayView.widthAnchor.constraintEqualToAnchor(mondayView.widthAnchor)
        ])

    let gradient = CAGradientLayer()
    gradient.frame = sundayView.bounds
    gradient.colors = [
        UIColor(red:1.00, green:0.37, blue:0.23, alpha:1.0).CGColor,
        UIColor(red:1.00, green:0.16, blue:0.41, alpha:1.0).CGColor
    ]
    sundayView.layer.insertSublayer(gradient, atIndex: 0)
}

你是不是忘记导入 QuartzCore 框架了? - Evgeny Karkan
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Ryan Cyrus
2个回答

16
你的渐变效果未显示,因为 sundayViewbounds 直到 Auto Layout 运行后才会被设置。在 viewDidLayoutSubviews 中重写并设置渐变框架。
将此属性添加到你的视图控制器中:
var sundayGradient: CAGradientLayer?

setupSundayView 中将 gradient 保存到属性中:

let gradient = CAGradientLayer()
sundayGradient = gradient

然后在viewDidLayoutSubviews中设置框架:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    sundayGradient?.frame = sundayView.bounds
}

抱歉,我有点理解不了“将渐变设置为它”的代码。能否请您再解释一遍呢? :) - Ryan Cyrus
1
setupSundayView函数中,在let gradient = CAGradientLayer()这一行后面添加一行代码sundayGradient = gradient - vacawama
谢谢!它起作用了 :) 但它破坏了我添加的另一行代码:sundayView.layer.cornerRadius = 5 - Ryan Cyrus
1
sundayView.clipsToBounds = true设置为true - vacawama

1
似乎您忘记在渐变层中添加 locations,请尝试按照以下方式进行:
func setupSundayView() {
    sundayView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activateConstraints([
        sundayView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor),
        sundayView.topAnchor.constraintEqualToAnchor(fridayView.bottomAnchor, constant: 16.0),
        sundayView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -8.0),            
        sundayView.heightAnchor.constraintEqualToAnchor(mondayView.heightAnchor),
        sundayView.widthAnchor.constraintEqualToAnchor(mondayView.widthAnchor)
        ])

    let gradient = CAGradientLayer()
    gradient.frame = sundayView.bounds
    gradient.colors = [
        UIColor(red:1.00, green:0.37, blue:0.23, alpha:1.0).CGColor,
        UIColor(red:1.00, green:0.16, blue:0.41, alpha:1.0).CGColor
    ]
    gradient.locations = [0.0, 0.5]
    sundayView.layer.insertSublayer(gradient, atIndex: 0)
}

好的,在你的 viewDidLoad 中尝试在 setupViews() 之前调用 setupSundayView() 函数。 - Evgeny Karkan
这是sundayView的框架。我尝试添加sundayView.frame.size = CGSize(width: 130, height: 130)。它起作用了。 - Ryan Cyrus
但是现在它正在覆盖我的 heightAnchor 和 widthAnchor。有没有办法在不定义大小的情况下初始化 frame? - Ryan Cyrus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接