How to add an initial viewer add-on added inside UIStackView

This is my setup: I have a UIScrollView with a leading, top, trial edge set to 0. Inside this I add a UIStackView with these restrictions:

 stackView.centerYAnchor.constraintEqualToAnchor(selectedContactsScrollView.centerYAnchor).active = true stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor).active = true 

Inside the stack view, I add some views.
My problem is that due to limitations, the first view added to the stack view will also have a leading edge = 0.

How can I add some additions to the first view? Without setting scroll view restrictions.

+83
ios swift uiscrollview uistackview
Sep 13 '15 at 16:29
source share
10 answers

The solution you provided does not add an addition to your views in your UIStackView (as you wanted in the question), but it does add a leadership position for UIStackView.

The solution may be to add another UIStackView to your original UIStackView and give rise to this new UIStackVIew. Then add your views to this new UIStackView.

Hint: You can do this completely with Interface Builder. In other words, there is no need to write code for it.

+20
Sep 15 '15 at 8:32
source share

When the isLayoutMarginsRelativeArrangement property is true, the stack view will compose its ordered views relative to its layout fields.

 stackView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) stackView.isLayoutMarginsRelativeArrangement = true 

But this affects all ordered views inside the stack view. If you want this add-on to be used for only one ordered view, you need to use the nested UIStackView

+268
09 Oct '15 at 6:19 06:19
source share

I found that the restrictions do not work inside the Stack View, or they seem a little strange.

(For example, if I add a start / end constraint to the image stack selected in the view, this will also add a start to the collection view, but not the end one; this will probably be a conflict).

stackview inside stackview

To set layout fields for all views within the stack, select:

Stack View > Size Inspector > Layout Margins > Fixed

Note. The "Fixed" option was previously called "Explicit" , as seen in the screenshots.

Then add your indent:

storyboard

+127
Jun 28 '16 at 17:10
source share

What worked for me was to add another UIView on the stack, which is just a spacer (works with at least stackView.distribution = .Fill):

 let spacerView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10)) stackView.addArrangedSubview(spacerView) stackView.addArrangedSubview(viewThatNeedsSpaceBeforeIt) stackView.addArrangedSubview(NextView)... 
+9
Nov 05 '15 at 8:10
source share

Use space to place items in the stack view:

  let bottomStackView = UIStackView() bottomStackView.alignment = .Center bottomStackView.axis = .Horizontal bottomStackView.spacing = 8.0 
+4
Nov 30 '15 at 11:29
source share

speed 3: you just need to set the offset to:

 firstView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 200).isActive = true 

Make sure this restriction is set after parentView.addArrangdSubView(firstView)

+4
Apr 6 '17 at 8:37
source share

If you only need overflow, you can set the glass alignment to "Trailing", and then you can specify unique Leading restrictions for each of its views contained in it.

As a bonus, you can also set the alignment of the stack view to "Center", and then you can use the Leading and / or Trailing constraints so that each element has its own addition on both sides.

+3
Nov 16 '16 at 21:17
source share

There are already good answers to this question, although there is one suggestion, use the spacing to set the distance between the views. There can be two options for the first and last views: either set the inserts according to the @tolpp sentence, or add a constraint bound to the parent (stackview) with a constant to add indentation.

+1
Jan 18 '19 at 4:08
source share

We have added transparent components (e.g. UIButton / UIView) as the first and last descendant of UIStackView. Then set the width limit of these invisible children to adjust the indentation.

0
Mar 20 '18 at 1:49
source share

It seems that the solution was quite simple. Instead:

 stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor).active = true 

I just wrote:

 stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor, constant: 15).active = true 
-one
Sep 13 '15 at 16:58
source share



All Articles