0

I am trying to add constraints programmatically for 3 different views but the height of the views is not adding properly.

     if tags.count == 0 && images.count == 0
        {          
                    
         addConstraintsWithFormat("H:|-05-[v0]-05-|", views: myTableView)
         addConstraint(NSLayoutConstraint(item: myTableView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 40))
         addConstraint(NSLayoutConstraint(item: myTableView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -5))
                    
         //2
         addConstraintsWithFormat("H:|-05-[v0]-05-|", views: bgView)
         addConstraint(NSLayoutConstraint(item: bgView, attribute: .top, relatedBy: .equal, toItem: titleLabel, attribute: .top, multiplier: 1.0, constant: 2))
          addConstraint(NSLayoutConstraint(item: bgView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -2))
              
      }
     else if tags.count != 0 && images.count == 0{

           //1
            addConstraintsWithFormat("H:|-05-[v0]-05-|", views: myTableView)
            addConstraint(NSLayoutConstraint(item: myTableView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 40))
            
            //2
            addConstraintsWithFormat("H:|-10-[v0]-05-|", views: tagsView)
            addConstraint(NSLayoutConstraint(item: tagsView, attribute: .top, relatedBy: .equal, toItem: myTableView, attribute: .bottom, multiplier: 1.0, constant: 15))
            addConstraint(NSLayoutConstraint(item: tagsView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -5))
            
            //3
            addConstraintsWithFormat("H:|-05-[v0]-05-|", views: bgView)
            addConstraint(NSLayoutConstraint(item: bgView, attribute: .top, relatedBy: .equal, toItem: titleLabel, attribute: .top, multiplier: 1.0, constant: 2))
            addConstraint(NSLayoutConstraint(item: bgView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -2))
                
    }
    else if tags.count != 0 && images.count != 0{

          //1
            addConstraintsWithFormat("H:|-05-[v0]-05-|", views: myTableView)
            addConstraint(NSLayoutConstraint(item: myTableView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 40))
            
            //2
            addConstraintsWithFormat("H:|-10-[v0]-05-|", views: tagsView)
            addConstraint(NSLayoutConstraint(item: tagsView, attribute: .top, relatedBy: .equal, toItem: myTableView, attribute: .bottom, multiplier: 1.0, constant: 5))
            addConstraint(NSLayoutConstraint(item: tagsView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -70))
            
            //3
            addConstraintsWithFormat("H:|-05-[v0]-05-|", views: picsCollectionView)
            addConstraint(NSLayoutConstraint(item: picsCollectionView!, attribute: .top, relatedBy: .equal, toItem: tagsView, attribute: .bottom, multiplier: 1.0, constant: 5))
            addConstraint(NSLayoutConstraint(item: picsCollectionView!, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -5))
            
            //4
            addConstraintsWithFormat("H:|-05-[v0]-05-|", views: bgView)
            addConstraint(NSLayoutConstraint(item: bgView, attribute: .top, relatedBy: .equal, toItem: titleLabel, attribute: .top, multiplier: 1.0, constant: 2))
            addConstraint(NSLayoutConstraint(item: bgView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -2))

}

extension UIView {
    
    func addConstraintsWithFormat(_ 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 ))
    }
}

enter image description here

6
  • What specific view's height is wrong? What did you expect it to be? Commented Feb 11, 2021 at 13:48
  • height is wrong for the checklist view and tags view i added background colors for better understanding. i want a fixed height according to the content Commented Feb 11, 2021 at 13:49
  • table views don't have an intrinsic height based on content. See: stackoverflow.com/questions/39626182/… Commented Feb 11, 2021 at 13:51
  • All of the orange tag view heights look right to me. If one of them is wrong, what exactly is wrong with it? Commented Feb 11, 2021 at 13:52
  • please check the code i added for the constraints. i didnt fix the height of either tagsview or the checklist tablview. the out put views are not overlapping but i want to remove the extra content which is coming after checklistTV and tagsview sometimes Commented Feb 11, 2021 at 13:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.