无法继承UiView的大小

时间:2017-03-02 21:27:06

标签: ios swift uiview gradient

我为自定义单元格创建了一个类,我想添加一个渐变图层。问题是当我硬编码新图层的大小时它工作正常,当我尝试从超级视图继承时不起作用。

这是我的代码:

class FirstCell: UICollectionViewCell {

    override init(frame: CGRect){
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let gradient: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false

        let layer = CAGradientLayer()
        layer.frame = view.bounds // This code is working: CGRect(x: 0, y: 0, width: 500, height: 200)

        layer.startPoint = CGPoint(x: 0, y: 0.5)
        layer.endPoint = CGPoint(x: 1, y: 0.5)
        layer.colors = [UIColor.lightGray.cgColor, UIColor.darkGray.cgColor]
        view.layer.addSublayer(layer)
        return view
    }()

    func setupViews() {
        addSubview(gradient)

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": gradient]))

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[v0]-15-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": gradient]))

    }

}

你能帮助我理解为什么这段代码layer.frame = view.bounds无效吗?

3 个答案:

答案 0 :(得分:0)

您新创建的视图没有大小!它没有约束,intrinsicContentSize,没有框架等等。

然后你取零尺寸并用它设置图层的框架。你只需将零放在那里,它就不会随着视图而改变

相反,您应该在视图具有有效大小后在函数中创建图层!

祝你好运

答案 1 :(得分:0)

我遇到了类似的问题并通过创建UIView的子类(在我的情况下为UIImageView)并覆盖layoutSubviews()

来解决它
class GradientView: UIView {

    override func layoutSubviews() {
        let layer = CAGradientLayer()
        layer.frame = view.bounds

        layer.startPoint = CGPoint(x: 0, y: 0.5)
        layer.endPoint = CGPoint(x: 1, y: 0.5)
        layer.colors = [UIColor.lightGray.cgColor, UIColor.darkGray.cgColor]
        view.layer.addSublayer(layer)
    }
}

当然会更改let gradient的类型以及您在该声明中返回的视图。 试试这个目前无法测试它。

答案 2 :(得分:0)

这是添加渐变的正确方法:

class FirstCell: UICollectionViewCell {

    override init(frame: CGRect){
        super.init(frame: frame)
        setupViews()
        setupGradientLayer()
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    let cellView: UIView = {

        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }()


    func setupViews() {

        addSubview(cellView)

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": cellView]))

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[v0]-15-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": cellView]))

    }



    private func setupGradientLayer() {

        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds

        gradientLayer.colors = [UIColor.clear.cgColor, UIColor.lightGray.cgColor]

        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)

        layer.addSublayer(gradientLayer)

    }

}