When activating a constraint, is the constraint relative to the view's parent view or the super view?

100 Views Asked by At

I have a three views:

  1. The superview
  2. A UIScrollView within superview
  3. A UIView within the UIScrollView

I was trying to define auto layout relations between the UIView and it's parent view, the UIScrollView, by using Visual Format strings through NSLayoutConstraint.constraints(withVisualFormat: ). However, when I activate the constraints via NSLayoutConstraint.activate(), the constraints appear to be relative to the superview rather than the parent view (UIScrollView) of UIView.

I was under the impression that the constraints were activated in relation to the view's parent view but I cannot find any documentation defining the relation between the view's parent view or the view's superview.

If the constraint is relative to the superview, is there any way to define the constraint relative to the parent view?

Thanks!

class MainVC: UIViewController {

var scrollView = UIScrollView()
var centerVC = CenterVC()

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.frame = CGRect(x: 0, y: 70, width: self.view.frame.width*0.8, height: self.view.frame.height - 120.0)
    self.view.addSubview(scrollView)

    centerVC.willMove(toParentViewController: self)
    self.addChildViewController(centerVC)
    centerVC.didMove(toParentViewController: self)

    let centerVCView: UIView = centerVC.view

    scrollView.addSubview(centerVCView)

    let slideViews: [String: UIView] = ["centerVCView" : centerVCView]
    let centerVCHorizontalConstraint = "H:|[centerVCView]|"
    let centerVCVerticalConstraint = "V:|[centerVCView]|"
    centerVCView.translatesAutoresizingMaskIntoConstraints = false

    var slideViewConstraints : [NSLayoutConstraint] = NSLayoutConstraint.constraints(withVisualFormat: centerVCHorizontalConstraint, options: [], metrics: nil, views: slideVCViews)
    slideViewConstraints += NSLayoutConstraint.constraints(withVisualFormat: centerVCVerticalConstraint, options: [], metrics: nil, views: slideVCViews)

    NSLayoutConstraint.activate(slideViewConstraints)
}}
0

There are 0 best solutions below