简体   繁体   中英

Adding UILabel to ScrollView Programmatically with AutoLayout (swift)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM