I am unable to get a stack view to fill the view canvas in order for my widgets to be automatically resized to fill the entire screen... i've seen this done on may tutorial/youtube video but for some strange reason it doesn't seem to work for me
How I went about it:
I have a view controller scene where I am trying to display three buttons layed out horizontally with three labels beneath them. I dragged the buttons and labels onto the view canvas from the object library and then added each button and it's label to a verticle stack view. Then I added the three verticle stack views to a horizontal stack view and set the distribution of the horizontal stack view to fill equally. I then control-dragged from the horizontal stack view to view in the view controller scene in storyboard and set the following constraints. (I zero'ed all the constants set automatically by storyboard and then have the following 4 constraints)
trailingMargin = Stack View.trailing
Stack View.leading = leadingMargin
Stack View.top = Top Layout Guide.bottom
Bottom Layout Guide.top = Stack View.bottom
When I run the app I don't see anything displayed on the simulator's iPhone screen and I get a message saying that one or more of the constraints could not be satisfied. Any idea what I am doing wrong?
Check Axis,Alignment and Distribution of the stack views. The outer stack has to have Horizontal,Centre, Equal Centring respectively and the inner stack view's need to be vertical,fill,fill.
Maybe your images are differently sizes, hence making the main stack view show content out of proportions.
I have tried to replicate your scene here.
Link To edited project.
I tried your Project.
Just removed your constraints and apply
Alignment constraints horizontally container and vertical container
at last use update constraints and I got the result on simulator.
It may help to solve your issue.
Related
I have been struggling with getting buttons (circular) to keep their size in a stack view to no avail.I have two rows of buttons where I place each row in its own horizontal stack view and then embed both stack views in an outer stack view to make it easier to set constraints.
The issue is that the buttons do not keep their original size in portrait or landscape. What settings am I missing to achieve this? I set the size of each button to w=50 and h=50 by resizing each button (no constraints)
Before embedding stack view:
Top row in a stack view:
Each button is of Type CircularButton
I set the size of each button to w=50 and h=50 by resizing each button (no constraints)
That is certainly one thing you are doing wrong: "no constraints". The stack view is a constraint maker. That is all it is. But it is not a mind-reader. It needs its arranged views to have certain constraints, under certain configurations, in order to know what you want. You have to tell it. Constraints are how you do that.
Let's stipulate that a lot of your question is a red herring — the roundedness of the buttons, the double set of stack views. All you want to know is how to make three buttons be spaced horizontally at equal distribution by a stack view without losing their size. Here's how.
First, configure your stack view like this:
Second, give your buttons height and width constraints, and wrap them in the stack view. Now use more constraints to position and size the stack view where you want the buttons distributed:
As you can see, that works as desired in the running app:
The rest of your interface merely builds upon that.
In the Attributes Inspector, you can set the alignment and distribution to center. This should work:
Also provide the same height and width for the buttons or put aspect ratio of 1:1 for the buttons.
I want to use a font size of 54 pt for labels in a stack view (that's embedded in other stack views), but the labels do not adjust their size and the top of the text is cut off or not shown at all. This doesn't happen if I choose a smaller font size. Does anyone know how to fix this? I am mostly using autolayout and the constraints in the interface builder.
Below is an image of the stack view configuration. A horizontal stack view with "00" and ":" is embedded in a vertical stack view with the label beneath it. And those four unities are embedded in another horizontal stack view.
I tried adding all the missing constraints but they made the whole app crash or the problem with the cut off number was still there so I have now removed them all (except constraints for the outer stack view.. it is only centered horizontally at the moment). I have now idea how I should go about creating the right constraints for the inner stack views...
I set up a viewcontroller in storyboard to suit your needs. Compare it to your setup:
I am trying to embed 4 UILabels and a UIView into a stack view. I selected all the labels then click the 'embed in stack' option. This then causes the labels to disappear from the view controller with ridiculous widths and X positions (e.g. one of the labels went from a width of 300 pts to 61,000,000 pts.) All the views I am trying to embed have correct constraints with each other and the rest of the view controller.
I've attached a photo of the views I am trying to embed in a stack (giving them background colours for visibility).
UI elements I want to embed in stack
All the views I am trying to embed have correct constraints with each other
There's your problem (at least in part). Do you understand what a stack view is? It is nothing but a shortcut for making constrains on its embedded views. It is a contraint maker.
For a stack view to work on your labels, therefore, your labels must have no constraints to one another. Rather it is the stack view itself that must be constrained (and the failure to do that sounds like the other half of your problem).
If u have a space when u want to put stackView, first put stackView, add constrains to that stackView and add the subviews on it.
I am trying to understand the basics of Auto Layout and have been trying to learn with no avail. In the image I have below I have set no constraints as I always end up not doing it correctly.
For the four squares and buttons I just want them to show appropriately on all size classes and I can not get them to. I always set leading constraints for the left squares/buttons and trailing constraints for the right squares/buttons(both to the main view controller). I put horizontal spacing and vertical spacing between the squares/buttons and put bottom/top spacing on the bottom/top squares/buttons to the bottom/top layout guide.
Screenshot:
First of all select your size class to for example w:compact h:regular and than do everything else.
You can do it using stack view. It is easy, just select two of your blue views (select one then hold shift and select second) and than click stack icon:
then set stack distribution to fill equally:
and set spacing for example to 8.:
than set constrains of the stack view to 0 from left and 0 from right and copy and paste. It will look like this:
then both stack view put in a stack view:
and set spacing to 8 in the main stack view.
Finally set 20 from bottom constraint to your main stack view.
You can use same way for buttons. After that you can simply set size class to all iPhones landscape (any,compact) and do same thing again.
If you do not understand anything, just ask me :).
I have a Vertical UIStackView with 7 elements inside. These elements are not collectively tall enough to fill the entire view, so they get spaced based on the distribution value. I would like the arranged views to stack at the top of the view, with no space at the top and empty space at the bottom. I would also like to be able to control the spacing below each view as well, if possible.
I have tried every available distribution option, none of them accomplish what I'm trying to do. Help is appreciated!
Update: Adding an image of my desired result to make it easier to understand
The views have variable heights (no problem, I have this working)
The views should appear in the order I add them (also working)
The functionality I want is this: The first view is anchored to the top of the stack view, the second view is anchored to the bottom of the first view, etc.
Have you tried using a stack view that embeds another stack view and an empty view. This could give you the results that you require. Create your view hierarchy this way.
Stackview
Stackview
FirstView
SecondView
ThirdView
....
SeventhView
PaddingView
By embedding your 7 views inside a stack view and then use a padding view and then embed the first stack view and the padding view inside another stack view you could achieve all the things that you want.