0

I have several views within a vertical stackview with leading alignment. And I want the button has 16 left spacing. I tried put a leading constraint but I get a conflict.

The 3 views you can see bellow are inside of the stackview.

enter image description here

I create a view and add the button as subview. Is it a good approach?

enter image description here

What should I've change?

Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • is your button located in stack view? – Suresh Mopidevi Oct 21 '19 at 09:42
  • Yes all the views are inside of the stackview. – Ricardo Oct 21 '19 at 09:45
  • 1
    Then how can you set constraint to `button`, you have to give leading constraint to `stackview`. Button will take only `height` and `width` or `aspect ratio` when it was in stack. – Suresh Mopidevi Oct 21 '19 at 09:45
  • I guess button has an intrinsic size. And the problem is the leading constraint and not de size. I'm missing something. – Ricardo Oct 21 '19 at 09:48
  • https://stackoverflow.com/a/53416570/9136962 – Suresh Mopidevi Oct 21 '19 at 09:53
  • Ok, I've set a fixed height and width constraint. But I still cant set a leading constraint without conflicts. – Ricardo Oct 21 '19 at 09:55
  • 2
    You have to embed the button in a `View` and account for that said leading constraint between the `Button` and the `View`. There is no direct way to define relative spacing constraint in a `StackView` environment other than the `Spacing` of the stack view. – nayem Oct 21 '19 at 10:15

1 Answers1

0

Alternative solutions

  1. Select button -> Embed in horizontal stackview
  2. add dummyview at 1st position of horizational stackview, set width constraint and set clear color
  3. play around alignment adjustments of horizontal stack view
  4. add space between dummyview and button

-- or --

suggested implementation from @nayem from comments.

  1. Remove button

  2. Add UIView as subview to stackview

  3. Add button as subview to UIView

  4. Add constraints to button

Community
  • 1
  • 1
Suresh Mopidevi
  • 919
  • 3
  • 9
  • 24
  • Is similar to add it as a subview and the apply a leadingconstraint. Is it a less nested approach? – Ricardo Oct 21 '19 at 10:02
  • 1
    Yes, it is.but using stack views makes sense than making complication with constraints. its up to you. if you have any better approach, post here. Thanks! – Suresh Mopidevi Oct 21 '19 at 10:07