I have a contentview inside a scrollview along with some other views, and scrollview height will change based on contentview height, and the height of the contentview is decided by the height of its subviews.
I am adding two subviews to contentview programmatically.
childView1 = CustomView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200))
childView2 = CustomView(frame: CGRect(x: 0, y: 210, width: self.view.frame.width, height: 200))
contentView.addSubview(childView1)
contentView.addSubview(childView2)
// childView1 top should be equal to contentView top
let topConstraint = NSLayoutConstraint(item: childView1, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 0)
// childView2 bottom should be equal to contentView bottom
let bottomConstraint = NSLayoutConstraint(item: childView2, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1, constant: 0)
// childView1 leading should be equal to contentView leading
let leadingConstraint1 = NSLayoutConstraint(item: childView1, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1, constant: 0)
// childView1 trailing should be equal to contentView trailing
let trailingConstraint1 = NSLayoutConstraint(item: contentView1, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1, constant: 0)
// childView2 leading should be equal to contentView leading
let leadingConstraint2 = NSLayoutConstraint(item: childView2, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1, constant: 0)
// childView2 trailing should be equal to contentView trailing
let trailingConstraint2 = NSLayoutConstraint(item: childView2, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1, constant: 0)
// Vertical Space between two child views is 10
let constraintTwoSubViews = NSLayoutConstraint(item: childView1, attribute: .bottom, relatedBy: .equal, toItem: childView2, attribute: .top, multiplier: 1, constant: 10)
contentView.addConstraints([topConstraint , bottomConstraint , leadingConstraint1,trailingConstraint1 , leadingConstraint2 , trailingConstraint2 , constraintTwoSubViews])
The problem is that the contentView height is not growing as per the subviews. Because of that my scroll views is not growing.
When I am adding second child view, I do not want to specify y = 210 , I want its position to be calculated by space constraint between two child views, Is it possible?
How can I make content view height grow as per its subviews?
translatesAutoresizingMaskIntoConstraintsto false for bothchildView1andchildView2like:childView1.translatesAutoresizingMaskIntoConstraints = false