UILabel appears in the debug view hierarchy, but not in the application

I have a UIView that I added to the ViewController via the Interface Builder.

In my code, I create a label in this UIView every time the button is clicked:

func addToDisplay(stringToDisplay: String) {
    let label = UILabel(frame: CGRectMake(0, 0, 10, 10))
    label.text = stringToDisplay
    label.textColor = UIColor.redColor()
    label.textAlignment = .Right
    label.sizeToFit()
    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.5
    label.setTranslatesAutoresizingMaskIntoConstraints(false)

    var lastLabel: UILabel?

    if displayView.subviews.count > 0 {
        lastLabel = displayView.subviews[displayView.subviews.count - 1] as? UILabel
    }

    displayView.addSubview(label)

    var viewsDictionary = ["displayView": displayView, "label": label]

    // add constraints
    if let lastLabel = lastLabel {
        viewsDictionary["lastLabel"] = lastLabel
        displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[label(==20)]-0-[lastLabel]", options: nil, metrics: nil, views: viewsDictionary))
    } else {
        displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[label(==20)]-0-|", options: nil, metrics: nil, views: viewsDictionary))
    }
    displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[label]-0-|", options: nil, metrics: nil, views: viewsDictionary)) 
}

      

When I click the button, nothing appears on my device (or simulator), but when I check the view debug hierarchy, it is there. I do not understand why. Any idea?

EDIT:

Here are two screenshots:

enter image description here

enter image description here

EDIT2:

Solved by changing the alpha to something other than 0 ...

+3


source to share


1 answer


Well I think you need to be told to update the constraints, write this and check:

[displayView updateConstraintsIfNeeded];

      



Also check that yours is displayView

added to self.view

! Are you using the navigation bar ? If so, what is it translucent

? If yes, then your self.view may start with (0,0) and the navigation bar . If that's the case, give multiple x and y , say (100, 100) and check if it shows up.

Let us know if this helped.

0


source







All Articles