title: How to add padding inside UITextField using NSLayoutConstraint in Swift?
body: <p>I have a subclass of <code>UIView</code> in which I have <code>UITextField</code> and wants to move placeholder text little right. I am using <code>NSLayoutConstraint</code> for setting subviews position and size. I have created <code>UIView</code> and adding it as <code>leftView</code> of <code>UITextField</code> but it crashes stating "The view hierarchy is not prepared for the constraint". Below is my code :</p>
  
  <pre><code>class MasterView: UIView {
  
  let searchTextField = UITextField()
  let paddingView = UIView()
  
  override init(frame: CGRect) {
      super.init(frame: frame)
  
      searchTextField.placeholder = "Search videos"
      searchTextField.translatesAutoresizingMaskIntoConstraints = false
      paddingView.translatesAutoresizingMaskIntoConstraints = false
      searchTextField.leftView = paddingView
      searchTextField.leftViewMode = UITextFieldViewMode.Always
  
      self.addSubview(searchTextField)
  
  }
  
  // Gets called when using storyboard
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  override func layoutSubviews() {
      self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 20))
  
      self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 0.12, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: searchTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Width, multiplier: 0.12, constant: 0))
  
      self.addConstraint(NSLayoutConstraint(item: paddingView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: searchTextField, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
  
  }
  </code></pre>
  
  <p>}</p>