How can implement stack view? - swift

I'm using UIStackView in my Storyboard, but I can't understand how I can set constrains in UIStackview. How to implement Stackview in Storyboard using Autolayout in Swift?

The question is really generic. Can you be more specific, what kind of layout your are trying to achieve with the StackView.
Anyhow let me try to answer:
how can set constrains in stack view ?
StackView is a container/layout, generally used to arrange other UIViews.
You only need to constraint the StackView to the container view, the child views that are added to the stack are managed based on Axis, Distribution, Spacing, etc.
Axis: Vertical or Horizontal orientation of child elements added to the stack.
Alignment: Aligns the child element parallel to the Axis.
Distribution: Aligns the child element perpendicular to the Axis.
Spacing: Inter element spacing.
How to implement stack view in storyboard using auto layout in swift ?
You can set all the parameters/values mentioned above in the storyboard from Attribute Inspector.
For auto layout enable the following option in your storyboard by selecting the specific controller.
For adding proper constraints that resizes, avoid using constants and concentrate more on relative constraints.
Properly explained in OFFICIAL DOCUMENT

Related

Image view crush stackview constraints in swift?

I have some issue about my stackview contraints or imageview constraints ı dont know actually.Probably both . This is my stackview constraints stackViewConst and this my imageview constraints imaviewConstraints . When ı try to run ı expect everything looks awesome however unfortunately ı get this screen screen my stackview is lost.How ı can figure out this help me
Answer: add stackview height constraint
(or add stack view content resistence and content hugging priorities higher than those of your UIImageView)
Explanation:
Since you are using storyboard's Interface builder (IB) for creating constraints, as opposed to pure code. The IB uses feature of auto-constraints, which basically means that the IB can figure itself the layout and constraints based on what is displayed in your storyboard. The UIStackView is using some dynamic resizing (so that elements within stackview are properly layed out) and it is always good idea to give it size constraints, or at least size constraints to elements inside UIStackView , otherwise it might decide that every element has size of CGSize(height:0,width:0) You can also make priorities of your UIStackView higher than those of your UIImageView so that IB would prioritize the size of your stackview's constraints to those of your imageView

How to add custom UIView to Stackview on top of UIScrollView programmatically?

I have a ViewController that has a UIScrollView with a vertical UIStackView on top of it.
Everything works fine if i add UIView to the stackview with height constraint trough the interface builder. For example 3 views with height 600px. If i run the app, i can scroll with no problem and see all three added views. The for the stackview works fine.
If i try to add a view to the stackview with code with addArrangedSubView trough code i can see that the first view that i added if i "force" scroll down, but not the other two. It doesnt matter if i set height constraints to the views that i add, and it doesnt matter if i set intrinsicContentSize for the custom views.
How can i get the scroll view to expand the scroll area when adding trough code. ?
Thanks :)
Just a guess in lack of more information:
The issue may be related to the way constraints are set up between the scroll view and its child, the stack view.
you should add the following constraints:
fix stack view edges (top, leading, trailing, bottom) to scroll view
set width constraint between stack view and scroll view
also set height constraint between stack view and scroll view, set its priority to low (250)
For more insights please read up on "setup UIStackView with auto layout", for example here:
https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7
If it doesn't work please provide the relevant code and screenshots of your Interface Builder setup.

How to make view controller expand to fit layout on smaller screens?

I am working on adding constraints to my view. I have everything perfectly laid out for the iPhone X and it looks great, but I would like my view to expand/scroll to accommodate smaller screens sizes. How can I transform my current view to scroll only if the screen size is not as tall as the iPhone X? ideally with as little reconfiguring as possible.
I follow this approach.
Put all your UI components inside a UIView say it ContainerView.
Now put ContainerView inside a UIScrollView.
Now select ContainerView, add constraints to make width and height equal as of scroll view.
Add a constraint of height on ContainerView. Set its relation as "Greater Than or Equal". Set constant value equal to the height which you want.
Set priority of equal height constraint to High.

How to set constraints to multiple UIImageView's in Swift

After hours of attempting to find a solution, as well as trying and failing myself I decided to ask here.
As shown on the picture below I have 15 Image views in a pyramid shape that I would like to set constraints on.
I have tried to:
Constraint equal width and height on all images
Constraint center image on line 1, 3 and 5 to horizontal center
Align vertical centers to all images on each line
Etc etc
The result is always the image view going out of bounds or similar issues. (Clip to Bounds is true)
Anyone have a solution or suggestions?
try putting them all in stack views then putting all those stack views inside a stack view and putting constraints on that
like this take a look at the view hierarchy on the left
to get the spacing i achieved just adjust the spacing on the right inside the attributes inspector
and its even able to have constraints on the highest stack view
all the errors go away once you add constraints to the top stack view
like shown here
Embed all your imageViews in a UIView.
set the top, bottom, leading and trailing constraint inside that view (top image to the superview, etc.).
align the UIView horizontally and vertically in the container.
You probably also want to set a width to your UIView, but I let you think about the final result you want.

Stack View not properly aligning

I have this layout using stack view, but when run in simulator it doesnt quite scale well,
This is how i aligned them in the scene.
Each of the views inside the stackView should have aspectRatio constraint set.
Alse don't forget the set your stackView in the following way: