title: How to use Visual Format Language to set constraints in Swift?
body: <p>I am having a View Controller which has a Nav Bar, I am trying to implement a Menu Bar right below the Nav Bar for which I have a separate class file(UIView). After adding the Menu Bar, I want to add an UIImageView exactly at the vertical centre of the Menu Bar View and not the Super View and have height as same as the Menu Bar View. I am using Visual Format Language. I am not really sure how to mention the view's name and how to constraint the image's height and place it in the centre of a named View. Can someone help me ?</p>
  
  <p>Below are code snippets...</p>
  
  <p>//MenuBar.swift</p>
  
  <pre><code>class MenuBar: UIView {
  
  
  override init(frame: CGRect) {
  
      super.init(frame: frame)
      print("created") 
  
      let imageLogo = UIImage(named: "Logo")
      let imageView = UIImageView(image: imageLogo)
      self.addSubview(imageView)
      self.addConstraintWithFormat("H:|[v0]|", views: imageView)
      self.addConstraintWithFormat("V:|[v0(100)]|", views: imageView)
      backgroundColor = UIColor.red
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  </code></pre>
  
  <p>//UIView Extension for the func <code>addConstraintWithFormat(- format:, views:)</code></p>
  
  <pre><code>extension UIView{
  func addConstraintWithFormat(_ format : String, views : UIView...) {
  
      var viewsDictionary = [String : UIView]()
  
      for(index, view) in views.enumerated(){
          let key = "v\(index)"
          view.translatesAutoresizingMaskIntoConstraints = false
          viewsDictionary[key] = view
      }
      addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary))
  
  }
  </code></pre>