简体   繁体   English

自定义元素的子层未在模拟器中呈现,在界面生成器中呈现

[英]Sublayer of custom element not rendering in simulator, renders in interface builder

I am trying to make a custom gradient layer that I can reuse in the Interface Builder for my different views. 我正在尝试创建一个自定义渐变层,可以在Interface Builder中为我的不同视图重复使用。 I created the following class: 我创建了以下类:

import UIKit 导入UIKit

@IBDesignable
class MyGradientLayer: UIView {

    let gradient = CAGradientLayer()

    public override init(frame: CGRect) {
        super.init(frame: frame)

        gradient.colors = [startColor.cgColor, endColor.cgColor]
        gradient.frame = self.bounds
        gradient.locations = [0, 1]
        self.layer.insertSublayer(gradient, at: 0)
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    @IBInspectable
    public var startColor: UIColor = UIColor.init(red: 1, green: 0.364, blue: 0.22352, alpha: 1.0) {
        didSet {
            gradient.colors = [startColor.cgColor, endColor.cgColor]
        }
    }

    @IBInspectable
    public var endColor: UIColor = UIColor.init(red: 1, green: 0.1607, blue: 0.40392, alpha: 1.0) {
        didSet {
            gradient.colors = [startColor.cgColor, endColor.cgColor]
        }
    }

    override func layoutSubviews() {
        gradient.frame = self.bounds
        gradient.locations = [0, 1]
        super.layoutSubviews()
    }

}

I then added a UIView to my ViewController and set the class to MyGradientLayer in the Identity inspector. 然后,我向我的ViewController添加了UIView,并在身份检查器中将类设置为MyGradientLayer。 My nice little gradient view then renders just fine in the Interface Builder. 然后,我的小渐变视图将在Interface Builder中很好地呈现。 I go ahead and set the constraints to take up the whole screen (I tried manual and auto constraints). 我继续并设置约束以占据整个屏幕(我尝试了手动和自动约束)。 All is well, right? 一切都好吧?

Nope, it does not render the gradient layer in the simulator. 不,它不会在模拟器中渲染渐变层。 However, I know that the view is there and is being put onto the screen, because I can set the background color of the UIView in IB, and it shows on the whole screen. 但是,我知道视图在那里并且正被放到屏幕上,因为我可以在IB中设置UIView的背景色,并且它会显示在整个屏幕上。

Any thoughts as to why the gradient layer inside my custom UIView is not rendering in the Simulator? 关于为什么我的自定义UIView中的渐变层没有在模拟器中呈现的任何想法?

Thanks! 谢谢!

See what happens if you change the "init" as follows: 查看如果按以下方式更改“ init”会发生什么:

public override init(frame: CGRect) {
    super.init(frame: frame)
    self.commonInit()
}

public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.commonInit()
}

func commonInit() {

    gradient.colors = [startColor.cgColor, endColor.cgColor]
    gradient.frame = self.bounds
    gradient.locations = [0, 1]
    self.layer.insertSublayer(gradient, at: 0)

}

Depending on how it's used, you may need to set a flag to prevent commonInit from being called more than once. 根据使用方式,您可能需要设置一个标志以防止commonInit被多次调用。 Give it a try and see. 试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM