title: Adding Constraint messed up view
body: <p>I have a view with a <code>UICollectionView</code> and a <code>UISegmentedControl</code>.</p>
  
  <p>I want to change constraints so the segment controller won't overlap collection view, like in this picture:</p>
  
  <p><a href="https://i.stack.imgur.com/uli3S.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uli3S.jpg" alt="enter image description here"></a></p>
  
  <p>This is my code :</p>
  
  <pre><code>override func viewDidLoad()
      {
          super.viewDidLoad()
  
          self.navigationItem.leftBarButtonItem = nil
          self.tabBarController?.tabBar.isHidden = true
  
  
          self.SegmentController.setTitle(SegmentAtext, forSegmentAt: 0)
          self.SegmentController.setTitle(SegmentBtext, forSegmentAt: 1)
  
  
          self.view.bringSubview(toFront: SegmentController)
  
          self.LoadProducts(productsToShow: SegmentAtype)
      }
  </code></pre>
  
  <p>SO I add this command:</p>
  
  <pre><code>self.ProductsCollection.topAnchor.constraint(equalTo: SegmentController.bottomAnchor, constant: 10).isActive = true
  </code></pre>
  
  <p>But the result is worse:</p>
  
  <p><a href="https://i.stack.imgur.com/wSBUM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wSBUM.jpg" alt="enter image description here"></a></p>
  
  <p>Now the segment controller is almost completely hidden!</p>
  
  <p>How do I fix this?</p>
  
  <p>Edit:</p>
  
  <p>My viewDidLayoutSubviews function:</p>
  
  <pre><code>override func viewDidLayoutSubviews()
      {
          ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
          let topConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20)
          let bottomConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50) //leaving space for search field
          let leadingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
          let trailingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
  
          self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
      }
  </code></pre>
  
  <p>Notice:</p>
  
  <p>my <code>viewDidLayoutSubviews</code> is implemented in the super view, which does not contain a <code>UISegmentedControl</code>. the <code>UISegmentedControl</code> is contained in an inheriting view.</p>
  
  <p>Edit: An updated view</p>
  
  <p><a href="https://i.stack.imgur.com/TqZ6K.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TqZ6K.jpg" alt="enter image description here"></a></p>