Programatically Created Label Within Container View Won't Expand For Text












1















I have a reusable view class, with the function .addDisapearingView() when added to another view displays the text in the functions parameters. Both the label and its container view are programmatically created. When there's long text in the label, I want the label, and the view to both grow in height. When there's text too long for the label, the label doesn't grow-and the text, subsequently, doesn't clip/go to next line. I'm trying to get the container view to expand programmatically based upon the text.



I've tried an extension that detects when the label is truncated. Using that extension, I used the += operator on the label and view to expand both of them with no luck.



while label.isTruncated {
print("printing while truncating in the while loop")
regView.frame.size.height += 5
label.frame.size.height += 5
}


The interesting thing with that is, I've used that code before, with the addition of adding 5 to the height constraint of the view in the storyboard to expand the size of the label for text, and it worked. That lead me to believe that my problem might reside somewhere in editing the height constraint for the regView.



I've tried countless variations of



    label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 3
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.frame.size.height = regView.frame.size.height
label.sizeToFit()
regView.layoutSubviews()


I've tried changing the frame of the view and label, changing the constaints at the top of the code, and the answers from other questions.



Code:



Truncated Label Extension:



extension UILabel {

var isTruncated: Bool {

guard let labelText = text else {
return false
}

let labelTextSize = (labelText as NSString).boundingRect(
with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [.font: font],
context: nil).size

return labelTextSize.height > bounds.size.height
}
}


View constraint changer:



extension UIView {

func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
constraint.constant = constant
self.layoutIfNeeded()
}
}
}


Whole function:



func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

regView.backgroundColor = colorView
regView.alpha = alpha
regView.frame = CGRect(x: toview.bounds.minX, y: toview.bounds.minY, width: toview.frame.size.width, height: height)

toview.addSubview(regView)

regView.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
let guide = toview.safeAreaLayoutGuide
regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
regView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
regView.heightAnchor.constraint(equalToConstant: height).isActive = true

} else {
NSLayoutConstraint(item: regView,
attribute: .top,
relatedBy: .equal,
toItem: toview, attribute: .top,
multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: regView,
attribute: .leading,
relatedBy: .equal, toItem: toview,
attribute: .leading,
multiplier: 1.0,
constant: 0).isActive = true
NSLayoutConstraint(item: regView, attribute: .trailing,
relatedBy: .equal,
toItem: toview,
attribute: .trailing,
multiplier: 1.0,
constant: 0).isActive = true
NSLayoutConstraint(item: regView, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
//regView.heightAnchor.constraint(equalToConstant: height).isActive = true
}

let label = UILabel(frame: CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.frame.width, height: height))
label.text = text
label.font = UIFont(name: "Arial", size: 12)
label.textColor = textColor
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 3
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.frame.size.height = regView.frame.size.height
label.sizeToFit()
regView.layoutSubviews()
regView.addSubview(label)
print("Label Height: (label.frame.height)")
print("Reg view height: (regView.frame.height)")

while label.isTruncated {
print("label is truncated")
regView.frame.size.height += 5
label.frame.size.height += 5
label.updateConstraint(attribute: NSLayoutAttribute.height, constant: regView.frame.height)
label.updateConstraint(attribute: NSLayoutAttribute.width, constant: regView.frame.width)

regView.layoutSubviews()
label.sizeToFit()
print("Label Height: (label.frame.height)")
print("Reg view height: (regView.frame.height)")
}

//remove
Timer.scheduledTimer(withTimeInterval: 2.8, repeats: false) { (action) in
UIView.animate(withDuration: 2.8, animations: {
self.regView.removeFromSuperview()
label.removeFromSuperview()

})
}

}


which is called by: ReusableView().addDisapearingView(toview: self.view, text: "Anonymous posts will still show up in your profile page!, more text text to test in teh view that doen't work!", textColor: UIColor.white, colorView: UIColor.darkGray, alpha: 0.9, height: 20)



The interesting thing(That I tried to fix) was that even if the height is set to 40, or a value where two lines of text could fit, the label still doesn't expand/truncate, much less if the height param is 20.



Any help would be greatly appreciated!










share|improve this question



























    1















    I have a reusable view class, with the function .addDisapearingView() when added to another view displays the text in the functions parameters. Both the label and its container view are programmatically created. When there's long text in the label, I want the label, and the view to both grow in height. When there's text too long for the label, the label doesn't grow-and the text, subsequently, doesn't clip/go to next line. I'm trying to get the container view to expand programmatically based upon the text.



    I've tried an extension that detects when the label is truncated. Using that extension, I used the += operator on the label and view to expand both of them with no luck.



    while label.isTruncated {
    print("printing while truncating in the while loop")
    regView.frame.size.height += 5
    label.frame.size.height += 5
    }


    The interesting thing with that is, I've used that code before, with the addition of adding 5 to the height constraint of the view in the storyboard to expand the size of the label for text, and it worked. That lead me to believe that my problem might reside somewhere in editing the height constraint for the regView.



    I've tried countless variations of



        label.adjustsFontSizeToFitWidth = true
    label.numberOfLines = 3
    label.lineBreakMode = .byWordWrapping
    label.translatesAutoresizingMaskIntoConstraints = false
    label.frame.size.height = regView.frame.size.height
    label.sizeToFit()
    regView.layoutSubviews()


    I've tried changing the frame of the view and label, changing the constaints at the top of the code, and the answers from other questions.



    Code:



    Truncated Label Extension:



    extension UILabel {

    var isTruncated: Bool {

    guard let labelText = text else {
    return false
    }

    let labelTextSize = (labelText as NSString).boundingRect(
    with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
    options: .usesLineFragmentOrigin,
    attributes: [.font: font],
    context: nil).size

    return labelTextSize.height > bounds.size.height
    }
    }


    View constraint changer:



    extension UIView {

    func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
    if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
    constraint.constant = constant
    self.layoutIfNeeded()
    }
    }
    }


    Whole function:



    func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

    regView.backgroundColor = colorView
    regView.alpha = alpha
    regView.frame = CGRect(x: toview.bounds.minX, y: toview.bounds.minY, width: toview.frame.size.width, height: height)

    toview.addSubview(regView)

    regView.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
    let guide = toview.safeAreaLayoutGuide
    regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
    regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
    regView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
    regView.heightAnchor.constraint(equalToConstant: height).isActive = true

    } else {
    NSLayoutConstraint(item: regView,
    attribute: .top,
    relatedBy: .equal,
    toItem: toview, attribute: .top,
    multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: regView,
    attribute: .leading,
    relatedBy: .equal, toItem: toview,
    attribute: .leading,
    multiplier: 1.0,
    constant: 0).isActive = true
    NSLayoutConstraint(item: regView, attribute: .trailing,
    relatedBy: .equal,
    toItem: toview,
    attribute: .trailing,
    multiplier: 1.0,
    constant: 0).isActive = true
    NSLayoutConstraint(item: regView, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
    //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
    }

    let label = UILabel(frame: CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.frame.width, height: height))
    label.text = text
    label.font = UIFont(name: "Arial", size: 12)
    label.textColor = textColor
    label.adjustsFontSizeToFitWidth = true
    label.numberOfLines = 3
    label.lineBreakMode = .byWordWrapping
    label.translatesAutoresizingMaskIntoConstraints = false
    label.frame.size.height = regView.frame.size.height
    label.sizeToFit()
    regView.layoutSubviews()
    regView.addSubview(label)
    print("Label Height: (label.frame.height)")
    print("Reg view height: (regView.frame.height)")

    while label.isTruncated {
    print("label is truncated")
    regView.frame.size.height += 5
    label.frame.size.height += 5
    label.updateConstraint(attribute: NSLayoutAttribute.height, constant: regView.frame.height)
    label.updateConstraint(attribute: NSLayoutAttribute.width, constant: regView.frame.width)

    regView.layoutSubviews()
    label.sizeToFit()
    print("Label Height: (label.frame.height)")
    print("Reg view height: (regView.frame.height)")
    }

    //remove
    Timer.scheduledTimer(withTimeInterval: 2.8, repeats: false) { (action) in
    UIView.animate(withDuration: 2.8, animations: {
    self.regView.removeFromSuperview()
    label.removeFromSuperview()

    })
    }

    }


    which is called by: ReusableView().addDisapearingView(toview: self.view, text: "Anonymous posts will still show up in your profile page!, more text text to test in teh view that doen't work!", textColor: UIColor.white, colorView: UIColor.darkGray, alpha: 0.9, height: 20)



    The interesting thing(That I tried to fix) was that even if the height is set to 40, or a value where two lines of text could fit, the label still doesn't expand/truncate, much less if the height param is 20.



    Any help would be greatly appreciated!










    share|improve this question

























      1












      1








      1








      I have a reusable view class, with the function .addDisapearingView() when added to another view displays the text in the functions parameters. Both the label and its container view are programmatically created. When there's long text in the label, I want the label, and the view to both grow in height. When there's text too long for the label, the label doesn't grow-and the text, subsequently, doesn't clip/go to next line. I'm trying to get the container view to expand programmatically based upon the text.



      I've tried an extension that detects when the label is truncated. Using that extension, I used the += operator on the label and view to expand both of them with no luck.



      while label.isTruncated {
      print("printing while truncating in the while loop")
      regView.frame.size.height += 5
      label.frame.size.height += 5
      }


      The interesting thing with that is, I've used that code before, with the addition of adding 5 to the height constraint of the view in the storyboard to expand the size of the label for text, and it worked. That lead me to believe that my problem might reside somewhere in editing the height constraint for the regView.



      I've tried countless variations of



          label.adjustsFontSizeToFitWidth = true
      label.numberOfLines = 3
      label.lineBreakMode = .byWordWrapping
      label.translatesAutoresizingMaskIntoConstraints = false
      label.frame.size.height = regView.frame.size.height
      label.sizeToFit()
      regView.layoutSubviews()


      I've tried changing the frame of the view and label, changing the constaints at the top of the code, and the answers from other questions.



      Code:



      Truncated Label Extension:



      extension UILabel {

      var isTruncated: Bool {

      guard let labelText = text else {
      return false
      }

      let labelTextSize = (labelText as NSString).boundingRect(
      with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
      options: .usesLineFragmentOrigin,
      attributes: [.font: font],
      context: nil).size

      return labelTextSize.height > bounds.size.height
      }
      }


      View constraint changer:



      extension UIView {

      func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
      if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
      constraint.constant = constant
      self.layoutIfNeeded()
      }
      }
      }


      Whole function:



      func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

      regView.backgroundColor = colorView
      regView.alpha = alpha
      regView.frame = CGRect(x: toview.bounds.minX, y: toview.bounds.minY, width: toview.frame.size.width, height: height)

      toview.addSubview(regView)

      regView.translatesAutoresizingMaskIntoConstraints = false
      if #available(iOS 11.0, *) {
      let guide = toview.safeAreaLayoutGuide
      regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
      regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
      regView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
      regView.heightAnchor.constraint(equalToConstant: height).isActive = true

      } else {
      NSLayoutConstraint(item: regView,
      attribute: .top,
      relatedBy: .equal,
      toItem: toview, attribute: .top,
      multiplier: 1.0, constant: 0).isActive = true
      NSLayoutConstraint(item: regView,
      attribute: .leading,
      relatedBy: .equal, toItem: toview,
      attribute: .leading,
      multiplier: 1.0,
      constant: 0).isActive = true
      NSLayoutConstraint(item: regView, attribute: .trailing,
      relatedBy: .equal,
      toItem: toview,
      attribute: .trailing,
      multiplier: 1.0,
      constant: 0).isActive = true
      NSLayoutConstraint(item: regView, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
      //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
      }

      let label = UILabel(frame: CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.frame.width, height: height))
      label.text = text
      label.font = UIFont(name: "Arial", size: 12)
      label.textColor = textColor
      label.adjustsFontSizeToFitWidth = true
      label.numberOfLines = 3
      label.lineBreakMode = .byWordWrapping
      label.translatesAutoresizingMaskIntoConstraints = false
      label.frame.size.height = regView.frame.size.height
      label.sizeToFit()
      regView.layoutSubviews()
      regView.addSubview(label)
      print("Label Height: (label.frame.height)")
      print("Reg view height: (regView.frame.height)")

      while label.isTruncated {
      print("label is truncated")
      regView.frame.size.height += 5
      label.frame.size.height += 5
      label.updateConstraint(attribute: NSLayoutAttribute.height, constant: regView.frame.height)
      label.updateConstraint(attribute: NSLayoutAttribute.width, constant: regView.frame.width)

      regView.layoutSubviews()
      label.sizeToFit()
      print("Label Height: (label.frame.height)")
      print("Reg view height: (regView.frame.height)")
      }

      //remove
      Timer.scheduledTimer(withTimeInterval: 2.8, repeats: false) { (action) in
      UIView.animate(withDuration: 2.8, animations: {
      self.regView.removeFromSuperview()
      label.removeFromSuperview()

      })
      }

      }


      which is called by: ReusableView().addDisapearingView(toview: self.view, text: "Anonymous posts will still show up in your profile page!, more text text to test in teh view that doen't work!", textColor: UIColor.white, colorView: UIColor.darkGray, alpha: 0.9, height: 20)



      The interesting thing(That I tried to fix) was that even if the height is set to 40, or a value where two lines of text could fit, the label still doesn't expand/truncate, much less if the height param is 20.



      Any help would be greatly appreciated!










      share|improve this question














      I have a reusable view class, with the function .addDisapearingView() when added to another view displays the text in the functions parameters. Both the label and its container view are programmatically created. When there's long text in the label, I want the label, and the view to both grow in height. When there's text too long for the label, the label doesn't grow-and the text, subsequently, doesn't clip/go to next line. I'm trying to get the container view to expand programmatically based upon the text.



      I've tried an extension that detects when the label is truncated. Using that extension, I used the += operator on the label and view to expand both of them with no luck.



      while label.isTruncated {
      print("printing while truncating in the while loop")
      regView.frame.size.height += 5
      label.frame.size.height += 5
      }


      The interesting thing with that is, I've used that code before, with the addition of adding 5 to the height constraint of the view in the storyboard to expand the size of the label for text, and it worked. That lead me to believe that my problem might reside somewhere in editing the height constraint for the regView.



      I've tried countless variations of



          label.adjustsFontSizeToFitWidth = true
      label.numberOfLines = 3
      label.lineBreakMode = .byWordWrapping
      label.translatesAutoresizingMaskIntoConstraints = false
      label.frame.size.height = regView.frame.size.height
      label.sizeToFit()
      regView.layoutSubviews()


      I've tried changing the frame of the view and label, changing the constaints at the top of the code, and the answers from other questions.



      Code:



      Truncated Label Extension:



      extension UILabel {

      var isTruncated: Bool {

      guard let labelText = text else {
      return false
      }

      let labelTextSize = (labelText as NSString).boundingRect(
      with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
      options: .usesLineFragmentOrigin,
      attributes: [.font: font],
      context: nil).size

      return labelTextSize.height > bounds.size.height
      }
      }


      View constraint changer:



      extension UIView {

      func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
      if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
      constraint.constant = constant
      self.layoutIfNeeded()
      }
      }
      }


      Whole function:



      func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

      regView.backgroundColor = colorView
      regView.alpha = alpha
      regView.frame = CGRect(x: toview.bounds.minX, y: toview.bounds.minY, width: toview.frame.size.width, height: height)

      toview.addSubview(regView)

      regView.translatesAutoresizingMaskIntoConstraints = false
      if #available(iOS 11.0, *) {
      let guide = toview.safeAreaLayoutGuide
      regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
      regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
      regView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
      regView.heightAnchor.constraint(equalToConstant: height).isActive = true

      } else {
      NSLayoutConstraint(item: regView,
      attribute: .top,
      relatedBy: .equal,
      toItem: toview, attribute: .top,
      multiplier: 1.0, constant: 0).isActive = true
      NSLayoutConstraint(item: regView,
      attribute: .leading,
      relatedBy: .equal, toItem: toview,
      attribute: .leading,
      multiplier: 1.0,
      constant: 0).isActive = true
      NSLayoutConstraint(item: regView, attribute: .trailing,
      relatedBy: .equal,
      toItem: toview,
      attribute: .trailing,
      multiplier: 1.0,
      constant: 0).isActive = true
      NSLayoutConstraint(item: regView, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
      //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
      }

      let label = UILabel(frame: CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.frame.width, height: height))
      label.text = text
      label.font = UIFont(name: "Arial", size: 12)
      label.textColor = textColor
      label.adjustsFontSizeToFitWidth = true
      label.numberOfLines = 3
      label.lineBreakMode = .byWordWrapping
      label.translatesAutoresizingMaskIntoConstraints = false
      label.frame.size.height = regView.frame.size.height
      label.sizeToFit()
      regView.layoutSubviews()
      regView.addSubview(label)
      print("Label Height: (label.frame.height)")
      print("Reg view height: (regView.frame.height)")

      while label.isTruncated {
      print("label is truncated")
      regView.frame.size.height += 5
      label.frame.size.height += 5
      label.updateConstraint(attribute: NSLayoutAttribute.height, constant: regView.frame.height)
      label.updateConstraint(attribute: NSLayoutAttribute.width, constant: regView.frame.width)

      regView.layoutSubviews()
      label.sizeToFit()
      print("Label Height: (label.frame.height)")
      print("Reg view height: (regView.frame.height)")
      }

      //remove
      Timer.scheduledTimer(withTimeInterval: 2.8, repeats: false) { (action) in
      UIView.animate(withDuration: 2.8, animations: {
      self.regView.removeFromSuperview()
      label.removeFromSuperview()

      })
      }

      }


      which is called by: ReusableView().addDisapearingView(toview: self.view, text: "Anonymous posts will still show up in your profile page!, more text text to test in teh view that doen't work!", textColor: UIColor.white, colorView: UIColor.darkGray, alpha: 0.9, height: 20)



      The interesting thing(That I tried to fix) was that even if the height is set to 40, or a value where two lines of text could fit, the label still doesn't expand/truncate, much less if the height param is 20.



      Any help would be greatly appreciated!







      swift dynamic view resize label






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 16:09









      Jack DerbisJack Derbis

      998




      998
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I guess you completely need auto-layout and make regView expand according to the label's text without any height constraints



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          regView.topAnchor.constraint(equalTo: guide.topAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          NSLayoutConstraint(item: regView,
          attribute: .top,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0).isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])

          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }


          Edit:



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          var topCon:NSLayoutConstraint!

          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          topCon = regView.bottomAnchor.constraint(equalTo: guide.topAnchor)
          topCon.isActive = true
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          topCon = NSLayoutConstraint(item: regView,
          attribute: .bottom,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0)
          topCon.isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])


          regView.layoutIfNeeded()

          topCon.constant += self.regView.frame.height

          UIView.animate(withDuration: 2) {
          toview.layoutIfNeeded()
          }


          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }





          share|improve this answer


























          • Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

            – Jack Derbis
            Jan 3 at 16:05











          • see edit .................

            – Sh_Khan
            Jan 3 at 16:28











          • Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

            – Jack Derbis
            Jan 3 at 23:38











          • yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

            – Sh_Khan
            Jan 3 at 23:39













          • Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

            – Jack Derbis
            Jan 4 at 0:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009559%2fprogramatically-created-label-within-container-view-wont-expand-for-text%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I guess you completely need auto-layout and make regView expand according to the label's text without any height constraints



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          regView.topAnchor.constraint(equalTo: guide.topAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          NSLayoutConstraint(item: regView,
          attribute: .top,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0).isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])

          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }


          Edit:



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          var topCon:NSLayoutConstraint!

          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          topCon = regView.bottomAnchor.constraint(equalTo: guide.topAnchor)
          topCon.isActive = true
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          topCon = NSLayoutConstraint(item: regView,
          attribute: .bottom,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0)
          topCon.isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])


          regView.layoutIfNeeded()

          topCon.constant += self.regView.frame.height

          UIView.animate(withDuration: 2) {
          toview.layoutIfNeeded()
          }


          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }





          share|improve this answer


























          • Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

            – Jack Derbis
            Jan 3 at 16:05











          • see edit .................

            – Sh_Khan
            Jan 3 at 16:28











          • Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

            – Jack Derbis
            Jan 3 at 23:38











          • yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

            – Sh_Khan
            Jan 3 at 23:39













          • Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

            – Jack Derbis
            Jan 4 at 0:13
















          1














          I guess you completely need auto-layout and make regView expand according to the label's text without any height constraints



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          regView.topAnchor.constraint(equalTo: guide.topAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          NSLayoutConstraint(item: regView,
          attribute: .top,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0).isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])

          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }


          Edit:



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          var topCon:NSLayoutConstraint!

          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          topCon = regView.bottomAnchor.constraint(equalTo: guide.topAnchor)
          topCon.isActive = true
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          topCon = NSLayoutConstraint(item: regView,
          attribute: .bottom,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0)
          topCon.isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])


          regView.layoutIfNeeded()

          topCon.constant += self.regView.frame.height

          UIView.animate(withDuration: 2) {
          toview.layoutIfNeeded()
          }


          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }





          share|improve this answer


























          • Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

            – Jack Derbis
            Jan 3 at 16:05











          • see edit .................

            – Sh_Khan
            Jan 3 at 16:28











          • Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

            – Jack Derbis
            Jan 3 at 23:38











          • yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

            – Sh_Khan
            Jan 3 at 23:39













          • Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

            – Jack Derbis
            Jan 4 at 0:13














          1












          1








          1







          I guess you completely need auto-layout and make regView expand according to the label's text without any height constraints



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          regView.topAnchor.constraint(equalTo: guide.topAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          NSLayoutConstraint(item: regView,
          attribute: .top,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0).isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])

          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }


          Edit:



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          var topCon:NSLayoutConstraint!

          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          topCon = regView.bottomAnchor.constraint(equalTo: guide.topAnchor)
          topCon.isActive = true
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          topCon = NSLayoutConstraint(item: regView,
          attribute: .bottom,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0)
          topCon.isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])


          regView.layoutIfNeeded()

          topCon.constant += self.regView.frame.height

          UIView.animate(withDuration: 2) {
          toview.layoutIfNeeded()
          }


          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }





          share|improve this answer















          I guess you completely need auto-layout and make regView expand according to the label's text without any height constraints



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          regView.topAnchor.constraint(equalTo: guide.topAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          NSLayoutConstraint(item: regView,
          attribute: .top,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0).isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])

          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }


          Edit:



          let regView = UIView()

          func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

          regView.backgroundColor = colorView
          regView.alpha = alpha
          toview.addSubview(regView)

          regView.translatesAutoresizingMaskIntoConstraints = false
          var topCon:NSLayoutConstraint!

          if #available(iOS 11.0, *) {
          let guide = toview.safeAreaLayoutGuide
          topCon = regView.bottomAnchor.constraint(equalTo: guide.topAnchor)
          topCon.isActive = true
          NSLayoutConstraint.activate([
          regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
          regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
          // regView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
          // regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          ])
          } else {
          topCon = NSLayoutConstraint(item: regView,
          attribute: .bottom,
          relatedBy: .equal,
          toItem: toview, attribute: .top,
          multiplier: 1.0, constant: 0)
          topCon.isActive = true
          NSLayoutConstraint(item: regView,
          attribute: .leading,
          relatedBy: .equal, toItem: toview,
          attribute: .leading,
          multiplier: 1.0,
          constant: 0).isActive = true
          NSLayoutConstraint(item: regView, attribute: .trailing,
          relatedBy: .equal,
          toItem: toview,
          attribute: .trailing,
          multiplier: 1.0,
          constant: 0).isActive = true
          // NSLayoutConstraint(item: regView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: toview, attribute: .height, multiplier: 1.0, constant: height).isActive = true
          //regView.heightAnchor.constraint(equalToConstant: height).isActive = true
          }

          let label = UILabel()
          label.text = text
          label.font = UIFont(name: "Arial", size: 12)
          label.textColor = textColor
          label.numberOfLines = 3
          label.lineBreakMode = .byWordWrapping
          label.translatesAutoresizingMaskIntoConstraints = false
          regView.addSubview(label)

          NSLayoutConstraint.activate([
          label.trailingAnchor.constraint(equalTo: regView.trailingAnchor),
          label.leadingAnchor.constraint(equalTo: regView.leadingAnchor),
          label.topAnchor.constraint(equalTo: regView.topAnchor),
          label.bottomAnchor.constraint(equalTo: regView.bottomAnchor) // this is the key behind expanding

          ])


          regView.layoutIfNeeded()

          topCon.constant += self.regView.frame.height

          UIView.animate(withDuration: 2) {
          toview.layoutIfNeeded()
          }


          Timer.scheduledTimer(withTimeInterval:3, repeats: false) { (action) in
          UIView.animate(withDuration: 2.8, animations: {
          self.regView.removeFromSuperview()
          })
          }

          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 23:46

























          answered Jan 2 at 16:37









          Sh_KhanSh_Khan

          46.4k51432




          46.4k51432













          • Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

            – Jack Derbis
            Jan 3 at 16:05











          • see edit .................

            – Sh_Khan
            Jan 3 at 16:28











          • Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

            – Jack Derbis
            Jan 3 at 23:38











          • yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

            – Sh_Khan
            Jan 3 at 23:39













          • Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

            – Jack Derbis
            Jan 4 at 0:13



















          • Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

            – Jack Derbis
            Jan 3 at 16:05











          • see edit .................

            – Sh_Khan
            Jan 3 at 16:28











          • Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

            – Jack Derbis
            Jan 3 at 23:38











          • yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

            – Sh_Khan
            Jan 3 at 23:39













          • Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

            – Jack Derbis
            Jan 4 at 0:13

















          Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

          – Jack Derbis
          Jan 3 at 16:05





          Is there a way to animate the height anchor so that when the method is used the view slides down into the superview?

          – Jack Derbis
          Jan 3 at 16:05













          see edit .................

          – Sh_Khan
          Jan 3 at 16:28





          see edit .................

          – Sh_Khan
          Jan 3 at 16:28













          Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

          – Jack Derbis
          Jan 3 at 23:38





          Thanks! Just for clarification, where are you getting the .view from...Do you mean regView?

          – Jack Derbis
          Jan 3 at 23:38













          yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

          – Sh_Khan
          Jan 3 at 23:39







          yes,,,,,,,,,,,,,,,, tested inside a vc not a subclass as you do

          – Sh_Khan
          Jan 3 at 23:39















          Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

          – Jack Derbis
          Jan 4 at 0:13





          Ok, I changed it to toView and it works(slides in). For some reason regView doesn't have the nice slide in motion. Thanks again tho

          – Jack Derbis
          Jan 4 at 0:13




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009559%2fprogramatically-created-label-within-container-view-wont-expand-for-text%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          ts Property 'filter' does not exist on type '{}'

          Notepad++ export/extract a list of installed plugins