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

Code cracker
  • 3,105
  • 6
  • 37
  • 67
  • What specific view's height is wrong? What did you expect it to be? – Lou Franco Feb 11 '21 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 – Code cracker Feb 11 '21 at 13:49
  • table views don't have an intrinsic height based on content. See: https://stackoverflow.com/questions/39626182/can-you-get-a-uitableviews-intrinsic-content-size-to-update-based-on-the-number – Lou Franco Feb 11 '21 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? – Lou Franco Feb 11 '21 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 – Code cracker Feb 11 '21 at 13:54
  • tables have no idea of how tall they are supposed to be. They don't do this based on the rows -- if you want that, you need to add code -- see the link I provided – Lou Franco Feb 11 '21 at 16:11

0 Answers0