Check this out I had one case like your one. Have a look - This will sure help you to resolve constraints conflicts.
Create UIScrollView
and set constrains with superview
// Create scrollview to display content
self.scrollView = UIScrollView()
self.scrollView.delegate = self
self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.scrollView)
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.Interactive
// Visual formatting constraints of scrollview horizontal-vertical alignment with respect to view
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView" : scrollView]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView" : scrollView]))
After creating scrollView, create subviews for scrollview and define its constraints:
[Here VariableView
is a different class, it is a subclass of UIView
it is not included with the code and it is just for reference. Replace your view with VariableView
]
func createQuestionControls() {
var previousControlView : VariableView! = nil
for variable in self.consultation.variables {
// Create custom view to add controls
var controlView : VariableView! = VariableView()
controlView.customTextDelegate? = self
controlView.setVariableView(variable as! Variable)
controlView.backgroundColor = UIColor.clearColor()
controlView.setTranslatesAutoresizingMaskIntoConstraints(false)
controlView.endEditing(true)
self.scrollView.addSubview(controlView)
// TOP Horizontal constraint
var metrices = ["width" : self.view.bounds.width]
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[controlView(width)]", options: NSLayoutFormatOptions(0), metrics: metrices, views: ["controlView" : controlView]))
if previousControlView == nil {
// Top vertical constraint
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[controlView]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["controlView" : controlView]))
} else {
// Top constraint to previous view
self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[previousControlView]-(10)-[controlView]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["controlView" : controlView, "previousControlView" : previousControlView]))
}
previousControlView = controlView
}
// This below constraints will increase UIScrollView content size
var constraint1 = NSLayoutConstraint.constraintsWithVisualFormat("H:[previousControlView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["previousControlView" : previousControlView])
self.scrollView.addConstraints(constraint1)
var constraint2 = NSLayoutConstraint.constraintsWithVisualFormat("V:[previousControlView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["previousControlView" : previousControlView])
self.scrollView.addConstraints(constraint2)
}
The point to be noted here is if you have multiple subviews in UIScrollView
then use bottom view (subview that is attached at last in scrollView), is used in constraint1
and constraint2
variables.