2

I am implementing a UIStackView and populating a UIStackView dynamically. But when there is a single view in UIStackView, the view is not centred in the UIStackView.

I use horizontal axis, center alignment and fill equality distribution in UIStackView. How can I start to populate UIStackView by putting views in center of the UIStackView?

Current behaviour of UIStackView

enter image description here

I want to start adding stars in the center of the UIStackView enter image description here

I am new to UIStackView sorry for an easy question.

I'm adding arranged views like;

tmpView = [[UIView alloc] init];
tmpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconStar"]];
[tmpView addSubview:tmpImageView];

[self.horizontalStackView addArrangedSubview:tmpView];

How can I achieve desired behaviour?

AfterCoder
  • 731
  • 2
  • 6
  • 22

1 Answers1

0

OK, it is as I thought. When you set the alignment of a UIStackView this works perpendicular to the axis of the stack view.

So in your case, the center means it will center the items from top-to-bottom (which it is doing).

In the direction of the axis the stack view will fill up from the top (in a vertical axis) or from the left (in a horizontal axis).

If you want your image to be centered like in your second screen shot then you could change the axis to .vertical and it would work. Without knowing what your end goal is I can't really recommend a specific solution.

If all you want is to align the single image this way then this should be fine.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Just like you said, I converted to vertical stackview. It worked for the first image but when I add the second image, it adds the second image below the first one. But I want to add it beside first one. – AfterCoder Dec 19 '16 at 13:23
  • @TheZipper OK, so then it wouldn't be centered... explain what your end goal is. If you drip feed questions then you will get drip feed answers. Just explain the end goal. No where in your question do you mention multiple images. – Fogmeister Dec 19 '16 at 13:24
  • @TheZipper try setting the `axis` to `horizontal` again but change the `distribution` to `equallyCentered`. I don't know what you're trying to achieve so no idea if this will work. – Fogmeister Dec 19 '16 at 13:26
  • OK, I'm sorry. I want to implement a horizontal container and add images to that container. I want images to start appearing in the center of container. When new image is added all images should be centered again and new image should appear beside of the previous one. – AfterCoder Dec 19 '16 at 13:28
  • @TheZipper ok, so what happens if there are 100 images in it? – Fogmeister Dec 19 '16 at 13:29
  • I will limit it to 5 so they will fit into container. Images wont be needed to resize. – AfterCoder Dec 19 '16 at 13:31
  • @TheZipper ok then. Try what I said in the comment. Set the axis back to `horizontal` and change the `distribution` to `equallyCentered`. – Fogmeister Dec 19 '16 at 13:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130960/discussion-between-thezipper-and-fogmeister). – AfterCoder Dec 19 '16 at 13:33