UIStackViews inside UIStackView giving errors - ios

I want to place 2 Horizontal UIStackViews inside another Vertical UIStackView. The main stack view is in the contentView of a UITableViewCell. Each of the nested stack views has 2 UIViews. The alignment is set to 'Fill' and distribution is 'Fill Equally'. For the main stack view both of them are set to 'Fill'. All of the UIViews has 2 elements, which are connected with constraints. When I place them in the storyboard I'm getting errors 'Need constraints for: Y position, height'. What am I doing here wrong?
EDIT: Also I'm hiding the UIViews depending if they have data from server or no. If both UIViews dont have data, then I hide the whole stackView.

You are missing vertical constraints for some of your views. Just because the stack view is set to "fill" does not mean the autoconstraints engine will know how tall to make each view. See if you can match the same constraints portrayed in the following screenshot and let me know if that solves your issue; these constraints make the views have equal height.

Need constraints for: Y position, height
this means that you place a view ( subviews of the inner stacks ) with no intrinsic content size so make sure to give it a height ( unlike label and button which already have ) and hook properly from top to bottom with their superviews

Related

How adjust the dynamically the height of a view based on height of stackview?

I have a problem with one of my VC. I need to resize a view depending on the height size of the stack view because the stack view have 2 textedit but in some cases one of them it's hidden.
Here is the view that contains the stackview this one needs to adjust the height depending on the height of the stackview
Here is the stackview contents, the stackview adjust perfectly depending if one of the textedit it's hidden or not but content view not, only leave the blank space under the stackview
These are the constraints of the container view.
Any suggestion to achieve the goal (?)
Please share the screenshot for constraints so I can guide you better?
What we do is we don't give any height constraint to the stack view. The views at the top and bottom then adjust themselves when the stack changes its height due to the subview hidden/show.

Why do nested UIStackViews with UILabels require Y position and height constraints?

I am trying to sort out a problem we have in a UITableViewCell. There are nested UIStackViews with subviews like UILabel having an intrinsic content size. I created this example for simplified explanation of my problem:
Results in these interface builder errors:
Top Stack
View Need constraints for: Y position, height
Middle Stack
View Need constraints for: Y position, height
Bottom Stack
View Need constraints for: Y position, height
To my (and my collegues) understanding, the stacks' and table view cell sizes should be determined by the UILabels and their intrinsic content size.
The "Outer Stack View" has four space constraints to its superview (the "Table View Cell") with 0 as constants because ultimately the table view cell and all stack view sizes should be based on the content views.
All UIStackView have Fill for Alignment and Distribution attributes.
I just dragged it together from the library as depicted, no fiddling in the inspector. It is simple to reproduce.
Adding 0-space top and bottom constraints to a label in relation to its parent stack view does not resolve the stack view allegedly lacking a height constraint.
The suggested constraints of the interface builder are nonsense.
What is missing? I created an example project hosted on GitHub.
You have to set the OutterStackView bottom constraint to be greater than or equal to the bottom of the cells content view.
See the issue is that you are asking the OutterStackView to be the same size as your cell in the StoryBoard but since your internal labels don't have their final intrinsic content size yet, you cannot fulfill all constraint requirements.

How to set an empty UIStackView inside a UIScrollView so that i can fill it on runtime?

I'm trying to set an empty vertical UIStackView inside a UIScrollView so that I can fill the UIStackView dynamically on runtime
I tried the solution given by apple here but when I do that, the Auto Layout tool tells me that there is a missing constraint which is either the Y position or the height of the UIStackView.
The only difference I have between my UIScrollView and the one from the example is that I don't add a button inside the UIStackView.
When I fill the UIStackView on runtime, the logs say it has to break some constraints and then the UIScrollView fills the screen all the way to the bottom, pushing other views I had there out of sight.
Is this the right way to accomplish what I want? Or is there any other solution?
EDIT:
This is what I have, the logo constraint is only aspect ratio relative to itself
Set up your constraints as you have them, but without the empty stack view in the scroll view.
Next:
add a UIStackView to the scroll view
constrain all 4 sides of the stack view to the scroll view
constrain the stack view width equal to the scroll view width
constrain the stack view height equal to the scroll view height
IB should show all constraints satisfied.
Then - and here's the key - edit the stack view's Height constraint and set it to be a Placeholder:
This Height constraint will now be removed at Build-Time. Any elements you add via code as arrangedSubviews will determine the height of the stack view, which will, in turn, define the vertical scrollable area (the .contentSize) --- all handled by auto-layout.

ios - How to auto-resize my view in storyboard? swift 3

I try to auto-resize my view with auto-layout contraints, but I have some errors of Y-position missing. I can't understand what is wrong, I attached my Y-position to the top of the view.
Thanks for your help, here is the screenshot:
The problem is that you don't set a fixed height but, if you can, ALWAYS AVOID to put a fixed height for a view. For an app that can fit all screen is better to use different techniques.
If don't want to set a fixed height on your view, you have two possible options:
Give to the view a bottom constraint. You should also set the distance between the bottom of your view and the screen (or another object below your view).
Use stackview. If you place your views into a stack view you will be able to autoresize the width and height of your views according to the screen dimension.
You must give a height for the view or construct it's subviews in a way that gives it a height by hooking constraints properly from top to bottom
when you have a y position error it means top, bottom and height issue
and when x it means leading , trailing and width issue
IF you want to create dynamic view drag a vertical UIStackView and give it a height and in runtime add items to it
self.myStackView.addArrangedSubview(lbl)

dynamic height of of scrollview subviews in autolayout ios

I am creating a UIScrollView from xib, in which 3 view are there 2 UIViews and in middle an UIImageView. when I am setting constraints Xcode asked to set Y position constrains. But the problem is Y position constraint is blocking Scrollview to scroll down and automatically adjusting the views which looks ugly in landscape mode.
when I am delete that constraint it ask to fix height of subview. I searched a lot but I am new in autolayout so not understanding many of solutions. any help would be great.
You have to set all the height constraints in the content view.
But you also want the height of the Content to be proportional to the screen size.
To do this assign the height constraint of the imageview [equal|proportional|a-computation-of] to the view containing the UISCrollView.
It seems weird to skip levels of herarchy when assigning constraints between two views whose are not direct ancestor/sibling of each other but within a scrollview (at least) it is perfectly acceptable.
You are basically telling the scrollview that it's content has a known size and at same time setting this content to adapt dinamically to the screen size (if the constraints of the root uiview are set correctly)
UIView1
|---UIScrollView
|---UIView2
|---UIImageView [heightConstr.constant=UIView1.height-UIView2.height-UIView3.height-margins]
|---UIView3
This is the basic idea, to be done programmatically, then you can explore other solutions.
Unfortunately the constraint system in ios pretty much sucks when it's up to more complex equations involving more views for a single constraint.
UIScrollViewcan be tricky when adding constraints. You should always add a subView that will behave as the content view for your UIScrollView and all your subsequent views will go inside this content view.
UIView1
|---UIScrollView
|---UIContentView
|---UIView2
|---UIImageView
Set your UIScrollViewconstraints as you would normally but set your content view to have leading, trailing, top and bottom to the UIScrollView but also add two more constraints which will be equal width and equal height to the viewController.view but will have a low priority (So that whichever direction your content will increase in, that constraint will break and automatically increase the content size of the scroll view by taking in the inferred height of the content view). Now go on and add constraints on all your subview as you normally would. Which i'm assuming would be:
Your topmost view will have top and leading and trailing to its superView and also a fixed height.
Your bottom view will have leading, trailing and bottom to its superView and also a fixed height.
Your UIImageViewwill have a leading, trailing and top to top most view and bottom to the bottom view.
Edit:
Here is the screenshot just in case (To show the view hierarchy with the content view's constraints in the inspector)

Resources