1

Now I try to insert a certain margin between views in StackView. But somehow there are constraints which I certainly didn't set. So, I assume a system create these unexpected conflicts. But I don't have confidence, so am I right?

The situation is like this.

The detail of this conflict is second one.top = first one.bottom. But I never create this constraint, but I did create second one.top = first one.bottom + 10. Why is the former constraint created? Is that StackView's nature? Thanks.

Kazuya Tomita
  • 667
  • 14
  • 34
  • Yes. Stack view will create few constraints for you. Again you added one more constraint. You don't need to add constraints for the views inside stack view. Stack view will take care of it. If you need to change these things change through attribute inspector. But don't add few more constraints on it. – Sivajee Battina May 04 '17 at 08:42
  • Thanks. In this case, the stackView adds the certain constraint about each view's space by the attribute `Spacing`. So, if I want to set some different margins toward subviews,namely make a distance between viewA and viewB 10 and make another distance between viewB and viewC 20, is it impossible? To me, it appears impossible using stackview. – Kazuya Tomita May 04 '17 at 08:59
  • You can do that using stack view spacing property. It will give equal spacing between views. If you want the variable spacing, follow this link: http://stackoverflow.com/questions/32999159/how-can-i-create-uistackview-with-variable-spacing-between-views – Sivajee Battina May 04 '17 at 09:08
  • To create variable spacing between views, you should be aware of most import properties of stack view, i.e, UIStackViewDistribution and UIStackViewAlignment. If you are not aware of them, read from apple docs: https://developer.apple.com/reference/uikit/uistackview – Sivajee Battina May 04 '17 at 09:11

1 Answers1

0

but I did create second one.top = first one.bottom + 10

You must not create any constraints for objects inside a stack view. It is the stack view's job to create constraints for you. That is what a stack view is. Use the stack view's settings so that it makes the constraints you want and positions the objects inside it where you want them.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You are overstating this. The [docs for UIStackView](https://developer.apple.com/reference/uikit/uistackview) even say "You can also fine tune an arranged view’s appearance by adding additional constraints to the arranged view. For example, you can use constraints to set a minimum or maximum height or width for the view. Or you can define an aspect ratio for the view." It's not without caveats, but contradicts your "*any*". – Ken Thomases May 06 '17 at 05:00
  • @KenThomases (Hi!) Yes, of course I thought of this even as I was writing. By using _internal_ constraints on a view, you give the stack view a clue as to how to set its constraints. And manipulating a view's intrinsic content size is another very important addition to your toolbox. Nonetheless, in the OP's case, the warning holds; I'm trying to be very clear that the stack view is the one in charge of making the constraints, and the conflict situation shown in the screen shot is do the OP interfering. – matt May 06 '17 at 05:04
  • @matt Although I can understand the whole things, but What is OP? – Kazuya Tomita May 06 '17 at 05:53
  • You are the OP, the original poster — the person who asked the question. – matt May 06 '17 at 13:15