Image view crush stackview constraints in swift? - 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

Related

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.

Why do StackViews not have intrinsic content size?

I've always wondered why UIStackviews don't have an intrinsic content size? What is Apple's reasoning for not having them calculate the intrinsic content size like a normal View does?
I use stackViews in autoresizing tableView cells, and I usually have to custom subclass the StackView to make the intrinsic content size be calculated correctly.
It does has an intrinsic content size for distribution type .fill but this needs the subviews to have intrinsic content size like label and button , otherwise give them a height , also make sure that you hook constraints properly from top to bottom inside the cell

How can implement stack view?

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

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.

autolayout constraint not working

I want to implement scrollview (Horizontal scroll)with paging and one of the page also contains another scrollview (Vertical scrollview) but elements in scrollview not follow autolayout constraints.Please suggest me way to implement it.
Here also attached image what i want to implement it.
Thanks
Nirav Zalavadia
Embed contents in the scrollview to a UIView. Set constraints to UIView withrespect to scrollview and set constraints to elements with respect to UIView.
While setting the constraints for UIScrollview and its container UIView, check whether the content size is setting correctly. Otherwise there is some problem with the constraints you have added.