Domanda

I have two buttons that I want to be kept the same size, but the problem is that they have two different parent views. Autolayout seems to be ignoring the "equal size" constraint in this scenario. Constraining buttons with the same parent view works just fine.

I've created a very simple example that depicts what I am seeing:

Constraints for One and Two Constraints for Three

As you can see from the above, buttons Two and Three are both set to have the same size constraint as button One. The only difference is that button Three is contained within another NSView. There are no width constraints that are linking button Three and its containing view.

However, when I run and resize the window, it looks like:

Running app

It doesn't matter whether I use the Interface Builder layout, or do it in code using the -[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] method.

What am I missing? Is this not supported by Autolayout? Thanks a lot.

Edited: Added screenshot to indicate button Three's constraints.

È stato utile?

Soluzione

This should work OK. I set up an example project to do the same thing. I have three buttons with equal width constraints between button one and button two. Then the same between button two and button three. Button one has a trailing edge constraint relative to it's parent view.

enter image description here

Note that between the second and third button, Interface Builder didn't let me do this in the main window. Instead, I had to do it using the document list on the left.

The result can be seen below:

enter image description here

Here's the link to the project:

https://github.com/MaxMacleod/ThreeButtonConstraintTest1

Couple of caveats. First, this is an iOS rather than an OS X project (I'm an iOS guy!). However the principles should be the same. Secondly, this doesn't pinpoint the exact reason why your project isn't working. However, if you can compare what this sample project does against yours, we can figure it out. I'll then update this answer. Better still if you could make your project available, I'd be happy to take a look.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top