title: In Swift, programmatically creating UIView and adding controls to it and using auto layout, causes the controls to appear on the view's parent
body: <p>I am trying to write a simple composite component for iOS in Swift 3. It consists of a UILabel followed by an UITextField laid out horizontally followed by a line under them. But What happens is the UILabel disappears, UITextField appears on the parent view and line also disappears.</p>
  
  <p><a href="https://i.stack.imgur.com/JWiui.png" rel="nofollow noreferrer">My design in sketch</a></p>
  
  <p><a href="https://i.stack.imgur.com/CzqGI.png" rel="nofollow noreferrer">What it actually looks like in the Storyboard</a></p>
  
  <p><a href="https://i.stack.imgur.com/RAYwZ.png" rel="nofollow noreferrer">My component's constraints in the view controller</a></p>
  
  <p>My intention was to use Auto Layout, anchor the label to top and leading anchors of the view, anchor the textfield to top of the view and trailing anchor of the label with a constant, so they would appear side by side.</p>
  
  <p>I did do a lot of research on this, one site that looked pretty close to what I wanted was <a href="https://www.raywenderlich.com/125718/coding-auto-layout" rel="nofollow noreferrer">https://www.raywenderlich.com/125718/coding-auto-layout</a>, and I think I am following more or less the same approach.</p>
  
  <p>I am doing something obviously wrong, but can't figure out what. Any help is much appreciated, I have been at this for a few days now.</p>
  
  <pre><code>import UIKit
  
  @IBDesignable
  class OTextEdit: UIView {
  
      @IBInspectable var LabelText: String = "Label"
      @IBInspectable var SecureText: Bool = false
      @IBInspectable var Color: UIColor = UIColor.black
      @IBInspectable var Text: String = "" {
          didSet {
              edit.text = Text
          }
      }
  
      fileprivate let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 35))
      fileprivate let edit = UITextField(frame: CGRect(x: 210, y: 0, width: 200, height: 35))
      fileprivate let line: UIView = UIView()
  
      override var intrinsicContentSize: CGSize {
          return CGSize(width: 300, height: 100)
      }
  
      func setup() {
          label.text = LabelText
          label.textColor = Color
          label.font = UIFont(name: "Avenir Next Condensed", size: 24)
          edit.font = UIFont(name: "Avenir Next Condensed", size: 24)
          edit.borderStyle = .roundedRect
          edit.isSecureTextEntry = SecureText
          line.backgroundColor = UIColor.white
  
          self.addSubview(label)
          self.addSubview(edit)
          self.addSubview(line)
      }
  
      override func willMove(toSuperview newSuperview: UIView?) {
          super.willMove(toSuperview: newSuperview)
          setup()
          setupConstaints()
      }
  
      func setupConstaints() {
          label.translatesAutoresizingMaskIntoConstraints = false
          edit.translatesAutoresizingMaskIntoConstraints = false
          line.translatesAutoresizingMaskIntoConstraints = false
          label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: -10).isActive = true
          label.topAnchor.constraint(equalTo: topAnchor)
          edit.leadingAnchor.constraint(equalTo: label.leadingAnchor, constant: 10).isActive = true
          edit.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
          edit.topAnchor.constraint(equalTo: self.topAnchor)
          line.heightAnchor.constraint(equalToConstant: 2.0).isActive = true
          line.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10).isActive = true
          line.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
          line.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 1.0).isActive = true
      }
  }
  </code></pre>