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])