user1107173
user1107173

Reputation: 10764

iOS8: viewDidLayoutSubviews() hides elements in Storyboard Auto-Layout

Setup:

I have a View Controller that consists of a View and a Container View.

I have setup the View and the Container View using class sizes.

The code below adds the gradient just fine:

   class ViewController: UIViewController {


    @IBOutlet weak var graphView: UIView!
    @IBOutlet weak var containerView: UIView!
    let backgroundColor = CAGradientLayer().graphViewBackgroundColor()

    override func viewDidLoad() {
        super.viewDidLoad()
   }

    override func viewDidLayoutSubviews() {

        super.viewDidLayoutSubviews()
        backgroundColor.frame = self.graphView.bounds
        self.graphView.layer.addSublayer(backgroundColor)

    }
}

Output:

enter image description here

There are no constraints laid out in code.

Problem:

However, I do have a label with black background that is not appearing. If I comment out the following lines, the label appears:

    //backgroundColor.frame = self.graphView.bounds
    //self.graphView.layer.addSublayer(backgroundColor)

Output:

enter image description here

Question:

Why is the gradient hiding the UILabel element that is in Auto-layout?

Upvotes: 0

Views: 117

Answers (1)

Sean
Sean

Reputation: 1534

Have you tried bringing the label to the front?

graphView.bringSubviewToFront(label)

Upvotes: 1

Related Questions