Sublayer not shown

0

Having this code:

private let topView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = UIColor(red: 216, green: 216, blue: 216)
    return view
}()

private lazy var typeLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textAlignment = .center
    label.text = self.searchType == SearchType.why ? "Why" : "What"
    label.font = UIFont.systemFont(ofSize: 28)
    return label
}()

private func setupViews() {
        self.view.addSubview(topView)
        self.topView.addSubview(typeLabel)

        self.view.addSubview(tableView)

        self.topView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.topView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
        self.topView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/8).isActive = true

        self.typeLabel.topAnchor.constraint(equalTo: self.topView.topAnchor, constant: 20).isActive = true
        self.typeLabel.leadingAnchor.constraint(equalTo: self.topView.leadingAnchor).isActive = true
        self.typeLabel.widthAnchor.constraint(equalTo: self.topView.widthAnchor, multiplier: 1/4).isActive = true

    }



 private func borderLayer(position: CGPoint?, size: CGSize?) -> CALayer {
        let borderLayer = CALayer()

        borderLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        borderLayer.name = "Border"

        borderLayer.backgroundColor = UIColor.red.cgColor
        borderLayer.position = position == nil ? CGPoint.zero : position!
        borderLayer.bounds.size = size == nil ? CGSize.zero : size!

        borderLayer.borderWidth = 5
        borderLayer.borderColor = UIColor.black.cgColor
        borderLayer.shadowOpacity = 1.0
        borderLayer.shadowOffset = CGSize(width: 20,height: 20)

        return borderLayer
    }

The problem is that the borderLayer of my typeLabel is not shown. The position and size are correct. I did the test putting the borderLayer to topView and it shows correctly, now the problem is when it is added to the daughter view of topView . What am I doing wrong so that the layer is not shown?

Full code: link

    
asked by MatiEzelQ 25.01.2017 в 19:24
source

1 answer

1

Modify the following parts of your code;

addViewBorderLayer (view: UIView)

private func addViewBorderLayer(view: UIView) {

    view.layoutIfNeeded() // Añade esto al principio de todo

    [...]

}

borderLayer (position: CGPoint? size: CGSize?)

private func borderLayer(position: CGPoint?, size: CGSize?) -> CALayer {

    let borderLayer = CALayer()
    borderLayer.anchorPoint = CGPoint(x: 1.0, y: 1.0)

    [...]

    borderLayer.position = CGPoint(x: size!.width, y: size!.height)

    [...]

}

Update 1

Changing those two lines, I get the following result:

    
answered by 26.01.2017 / 01:44
source