为什么UITableViewCell中的渐变层不能覆盖整个框架? [英] Why gradient layer in UITableViewCell not cover entire frame?

查看:114
本文介绍了为什么UITableViewCell中的渐变层不能覆盖整个框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用长UIView做一个标准的渐变上下.但是还不满.笔尖是UITableViewCell的一部分,所以我没有访问viewDidLayoutSubviews()的权限,就像此线程一样.

I try to make a standard gradient top-bottom with long UIView. But it's not full. The nib is part of UITableViewCell, so I don't have access to viewDidLayoutSubviews() as in this thread.

我尝试从此视图的代码版本调用contentView.layoutIfNeeded().我在调用UITableView cellForRowAtIndexPath时调用了它.但这没有效果.

I've tried to call contentView.layoutIfNeeded() from the code version of this view. I called it when UITableView cellForRowAtIndexPath gets called. But it's no effect.

我在awakeFromNib()中准备渐变.

let colors = [
            UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1.0).cgColor,
            UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0).cgColor]
let gradient = CAGradientLayer()
gradient.frame = gradientView.bounds
gradient.colors = colors
gradientView.layer.insertSublayer(gradient, at: 0) 

我的代码有问题吗?

推荐答案

您应该使用视图的bounds而不是frame,因为您的图层在视图内部,并且框架可能有偏移.

You should use view's boundsinstead of frame, because your layer is inside of the view, and the frame may have an offset.

awakeFromNib之后更改了视图的布局.因此,您应该在视图的layoutSubviews中调整图层的大小.为此,创建一个属性gradient和:

The layout of the view is changed after awakeFromNib. So you should resize the layer in layoutSubviews of the view. For this create a property gradient and:

let gradient: CAGradientLayer = CAGradientLayer()

override func awakeFromNib() {
    ...
    let colors = [
                UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1.0).cgColor,
                UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0).cgColor]
    gradient.frame = bounds
    gradient.colors = colors
    gradientView.layer.insertSublayer(gradient, at: 0) 
    ...
}

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

编辑:另一种选择是使用具有自己的图层类的自定义视图:

EDIT: An alternative is a custom view with own layer class:

public class GradientLayer: UIView {
    @IBInspectable var startColor: UIColor! = UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1.0)
    @IBInspectable var endColor: UIColor! = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)

    override class var layerClass : AnyClass {
        return CAGradientLayer.self
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        let colors = [ startColor.cgColor, endColor.cgColor ]
        if let gradient = self.layer as? CAGradientLayer {
            gradient.colors = colors
        }
    }
}

这更优雅,因为您可以用 IB inspectables 替换静态颜色,并且具有可重复使用的组件视图.

This is more elegant, because you can replace the static colors with IB inspectables, and you have a reusable component view.

这篇关于为什么UITableViewCell中的渐变层不能覆盖整个框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆