I am trying to simply center a my GridView
with Autolayout
constraints programmatically. The answer that have been suggested here and here recommend using
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
toItem: self.gridView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0)
I am adding the constraints like so:
func setupConstraints() {
let centerX = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
toItem: self.gridView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0)
let centerY = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
toItem: self.gridView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0)
self.view.addConstraints([centerX, centerY])
}
However, this doesn't seem to work for me. When running the app, the view is positioned on the top left corner and the console shows wild output about issues with simultaneously satisfying the constraints.