Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法继承UiView的大小_Ios_Swift_Uiview_Gradient - Fatal编程技术网

Ios 无法继承UiView的大小

Ios 无法继承UiView的大小,ios,swift,uiview,gradient,Ios,Swift,Uiview,Gradient,我为自定义单元创建了一个类,我想添加一个渐变层。问题是,当我硬编码新层的大小时,它工作正常,而当我尝试从超级视图继承时,它不工作 这是我的密码: class FirstCell: UICollectionViewCell { override init(frame: CGRect){ super.init(frame: frame) setupViews() } required init?(coder aDecoder: NSCode

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

这是我的密码:

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
不起作用吗

新创建的视图没有大小!它没有约束、intrinsicContentSize、没有帧集等

然后取其零大小,并设置层的帧。你只要把零放进去,它就不会随视图而改变

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


祝你好运

我遇到了一个类似的问题,通过创建UIView的子类(在我的例子中是UIImageView)并覆盖layoutSubviews()解决了这个问题

当然,还要更改
let gradient
的类型以及在该声明中返回的视图。
尝试此操作当前无法测试它。

以下是添加渐变的正确方法:

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)

    }

}

有道理。但我是在创建视图中的自动布局约束后定义大小的。我应该如何重构代码以找到正确的位置来创建层。也许在didMoveToWindow,或者didLayout或者其他什么东西中,我只是从内存中写的,很抱歉,请尝试在代码中添加view.setNeedsDisplay()view.layoutIfNeeded()@nikdange_me我试图在创建视图变量后添加它,但它没有帮助您在{layer.frame=view.bounds}代码之前添加它?@nikdange_me yes。。两条线。。就在let view=UIView()layoutifneed和setNeedsDisplay之后,在addSubview(渐变)中将视图添加为子视图之前,不会执行任何操作
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)

    }

}