0

I've got a scrollview and inside it, an imageview in storyboard. I have iboutlets to the file this code is in with references to those views. They are called scrollView and graph respectively. I am trying to programmatically add a lot of UILabels below the graph inside the scrollview, but I can't even get one to show up.

The width constraint I added doesn't cause any problems, but the other three constraints get runtime errors saying The view hierarchy is not prepared for the constraint. I want to constrain the top of the label to the bottom of the graph, the left of the label to the left of the scrollview, and the bottom of the label to the bottom of the scrollview. What am I doing wrong?

Updated Code:

  var noteBodyLabel = UILabel()
  noteBodyLabel.text = "test asdf  asd fasdf a sdf asdf as dfa sdf  safd"

  noteBodyLabel.setTranslatesAutoresizingMaskIntoConstraints(false)



  var widthCons = NSLayoutConstraint(
     item: noteBodyLabel,
     attribute: .Width,
     relatedBy: .Equal,
     toItem: nil,
     attribute: .NotAnAttribute,
     multiplier: 1, constant: 280)


  let topCons = NSLayoutConstraint(
     item: noteBodyLabel,
     attribute: .Top,
     relatedBy: .Equal,
     toItem: graph,
     attribute: .Bottom,
     multiplier: 1, constant: 0);


  let bottomCons = NSLayoutConstraint(
     item: noteBodyLabel,
     attribute: .Bottom,
     relatedBy: .Equal,
     toItem: scrollView,
     attribute: .Bottom,
     multiplier: 1, constant: 0);

  let leftCons = NSLayoutConstraint(
     item: noteBodyLabel,
     attribute: .Left,
     relatedBy: .Equal,
     toItem: scrollView,
     attribute: .Left,
     multiplier: 1, constant: 0);

  scrollView.addSubview(noteBodyLabel)
  noteBodyLabel.addConstraints([topCons,leftCons,bottomCons,widthCons])
Max Hudson
  • 9,961
  • 14
  • 57
  • 107
  • 2
    The errors are because you're creating constraints between two views that aren't in the same tree. Call `scrollView.addSubview(noteBodyLabel)` before adding the constraints. – Aaron Brager Feb 04 '15 at 18:01
  • @AaronBrager look at my updated code - It's still happening – Max Hudson Feb 04 '15 at 21:20
  • 'not prepared for the constraint' means that one view in your constraints has not been added into the view hierarchy. Are you sure all of the views have been added as subviews? – Chris Truman Feb 04 '15 at 21:42
  • @ChrisTruman I mean I know graph is a valid subview of scrollView and I know scrollView is a valid subview of self.view. Both graph and scrollView are added via storyboard and my code is running in viewDidLoad. Could that have something to do with it? – Max Hudson Feb 05 '15 at 01:32
  • it may be that viewDidLoad is not the right time to add the constraints: http://stackoverflow.com/a/19398589/150920 – Chris Truman Feb 05 '15 at 05:22
  • See the answer I left myself – Max Hudson Feb 05 '15 at 06:55

1 Answers1

0

I found that I had to add the constraints to self.view, not note1 or any of the other elements.

  var note1 = UILabel()
  note1.text = "test asdf  asd fasdf a sdf asdf as dfa sdf  safd"

  note1.setTranslatesAutoresizingMaskIntoConstraints(false)
  scrollView.addSubview(note1)

  var widthCons = NSLayoutConstraint(
     item: note1,
     attribute: .Width,
     relatedBy: .Equal,
     toItem: nil,
     attribute: .NotAnAttribute,
     multiplier: 1, constant: 280)


  let topCons = NSLayoutConstraint(
     item: note1,
     attribute: .Top,
     relatedBy: .Equal,
     toItem: graph,
     attribute: .Bottom,
     multiplier: 1, constant: 0);

  let bottomCons = NSLayoutConstraint(
     item: note1,
     attribute: .Bottom,
     relatedBy: .Equal,
     toItem: scrollView,
     attribute: .Bottom,
     multiplier: 1, constant: 0);

  let leftCons = NSLayoutConstraint(
     item: note1,
     attribute: .Left,
     relatedBy: .Equal,
     toItem: scrollView,
     attribute: .Left,
     multiplier: 1, constant: 0);

  self.view.addConstraints([topCons, bottomCons, leftCons, widthCons])
Max Hudson
  • 9,961
  • 14
  • 57
  • 107