1

I have stackview with four elements that need to be centered horizontally:

let stackView = UIStackView()
stackView.axis = UILayoutConstraintAxis.vertical
stackView.distribution  = UIStackViewDistribution.equalSpacing
stackView.alignment = UIStackViewAlignment.center
stackView.spacing = 5.0
stackView.addArrangedSubview(browseButton)
stackView.addArrangedSubview(orLabel)
stackView.addArrangedSubview(createButton)
stackView.addArrangedSubview(imageView)
let imageViewCenterXConstraint = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: stackView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
stackView.addConstraint(imageViewCenterXConstraint)
stackView.translatesAutoresizingMaskIntoConstraints = false

let stackViewCenterXConstraint = NSLayoutConstraint(item: stackView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: wantMorePage!, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
let stackViewCenterYConstraint = NSLayoutConstraint(item: stackView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: wantMorePage!, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
wantMorePage?.backgroundColor = UIColor.gray

wantMorePage!.addSubview(stackView)

wantMorePage!.addConstraint(stackViewCenterXConstraint)
wantMorePage!.addConstraint(stackViewCenterYConstraint)

self.scrollView.addSubview(wantMorePage!)

I added constraints and set alignment to center, but elements are still not centered (screenshot). What should I do to center them?enter image description here

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
Elena Rubilova
  • 367
  • 4
  • 16

2 Answers2

0

Replace:

wantMorePage!.addConstraint(stackViewCenterXConstraint)
wantMorePage!.addConstraint(stackViewCenterYConstraint)

With:

stackViewCenterXConstraint.isActive = true
stackViewCenterYConstraint.isActive = true

https://developer.apple.com/documentation/uikit/uiview/1622523-addconstraint

Also you need to add the subView before you can add constraints to it

Move this above isActive calls:

self.scrollView.addSubview(wantMorePage!)

Like so:

wantMorePage!.addSubview(stackView)
self.scrollView.addSubview(wantMorePage!)

let imageViewCenterXConstraint = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: stackView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
imageViewCenterXConstraint.isActive = true

let stackViewCenterXConstraint = NSLayoutConstraint(item: stackView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: wantMorePage!, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
let stackViewCenterYConstraint = NSLayoutConstraint(item: stackView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: wantMorePage!, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)

stackViewCenterXConstraint.isActive = true
stackViewCenterYConstraint.isActive = true
Harry Singh
  • 826
  • 5
  • 11
0

Found what the issue was - the width of the view was incorrect, in my code wantMorePage was bigger than needed, I made it the same size as its superview and it fixed the issue.

Elena Rubilova
  • 367
  • 4
  • 16