1

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.

enter image description here

Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132
  • What other constraints are active in your view? – Stonz2 Dec 08 '15 at 16:12
  • none so far... I want to add one for size later using visual format language `[gridView(100@100)]`, but it's not in there at the moment. – nburk Dec 08 '15 at 16:15
  • "and the console shows wild output about issues with simultaneously satisfying the constraints" And that's the problem. It isn't "wild"; it's extremely informative. You need to _read_ that output. If you can't, then _quote_ it in your question. Stop flailing, start thinking. – matt Dec 08 '15 at 16:23

2 Answers2

3

This should work:

func setupConstraints() {

    self.viewToCenter.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true

    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true
}

Your first and second items were wrong and don't use addConstraints because it is or will be deprecated (iOS <= 8).

DevAndArtist
  • 4,971
  • 1
  • 23
  • 48
  • this works, I actually think the issue was that I didn't set `self.viewToCenter.translatesAutoresizingMaskIntoConstraints = false`... however, the centering still doesn't work with this code. to make the example even simpler, I replaced my `GridView` with a simple `UIView`, but it still doesn't work (actually, the view doesn't show up at all, it's not even there when I use _view debugging_ in Xcode...). I guess I should talk about that in another question though. – nburk Dec 08 '15 at 16:51
  • Have you ever tried to set width/height constrains? AutoLayout may break normal frames from time to time! – DevAndArtist Dec 08 '15 at 17:00
  • I did, using `NSLayoutConstraint.constraintsWithVisualFormat("[gridView(100@100)]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["gridView": self.gridView])`,... the strange thing is, when I log the frame before and after `setupConstraints()`, the output **both times** is `>`, except that the view has disappeared – nburk Dec 08 '15 at 17:06
  • Btw. this is the old AutoLayout syntax. Try to use 'self.vieToCenter.centerXAnchor.constraintEqualsAnchor(self.view.centerXAnchor).active = true' – DevAndArtist Dec 08 '15 at 17:08
  • here's the new question btw http://stackoverflow.com/questions/34161709/centering-a-uiview-programmatically-using-autolayout-makes-the-view-disappear-fr – nburk Dec 08 '15 at 17:08
  • 1
    The way the problem was solved was like I told you here. AutoLayout may break the frame of your view so sometimes it is better to create size constraints. If you have some time to experiment and to learn a little, you might want to try out my little syntax wrapper for the new AutoLayout syntax in Swift: https://github.com/DevAndArtist/NSLayoutConstraint-Extension – DevAndArtist Dec 08 '15 at 19:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97324/discussion-between-devandartist-and-nburk). – DevAndArtist Dec 08 '15 at 19:47
1

I think you don't add this code

self.gridView.translatesAutoresizingMaskIntoConstraints = false
vien vu
  • 4,277
  • 2
  • 17
  • 30