how to prevent buttons from shrinking in a stackview - ios

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.

Related

Font size too big for label in stack view

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:

Position and size are ambiguous for “view”

I have set pin to all view but i got continuously this alert !
I have seen that this is because of height and width constraints but i didn't add height and width still i got this error !
This alert should be avoidable or not.
A UIView has no intrinsic size meaning none of your views know how large they should be and thus cannot be laid out the way you have them. You can set the height of each one to a fixed number but if you want them to scale I recommend using Equal Heights and Equal Widths and set the multiplier to the proportion of the view you want the little views to size to be. You could do this so many ways.
I am going walk you through one way that I think is the quickest(using StackViews) for my own sanity but the same logic could work with more dragging from each view.
Step 1:
Drag a Vertical StackView to the top of the storyboard. Add the following constraints. pin leading, trailing, and top =20. Then drag from the vertical stack view to the main view and choose equal heights and change the multiplier to 0.6(60% of the main view height). See image
Step 2:
Add a single horizontal stackview as an arranged Subview to our vertical stack view. Now add 3 UIViews and change the colors to desired colors. Change stack view to Alignment-Fill and Distribution-Fill Proportionately. Also add spacing=20. See Image
Step 3:
Choose your horizontal stackview that you just created in step 2 and hit Command-C to copy it. Hit Command-V to paste it. Note- If it does not paste it into the vertical stackview drag it in to the vertical stack view. Not there yet but close. See image
Step 4: Go to the vertical stack view and change it to Alignment-Fill and Distribution-(Fill Equally). Add Spacing of 20. You should now be a point that looks like this. See image
Step 5. Add a horizontal stack view below the vertical stack view and the bottom layout guide. Pin to all four sides at 20. Add three views and change the color to desired color. Change Alignment-Fill, Distribution-Fill Equally, and spacing=20.
Step 6. Adjust verticalStackView equal heights multiplier to maybe a lower number(0.5) to make it look like your view.
Step 7. Reap the rewards
The Big takeaway is that a UIView needs to know how big it is. UIStackView in this case tells the views how big they are. You could just have easily set the height and width of one of your views in your screenshot above to a percentage of the view. Then drag from that view to all of the like sized views and set equal heights/widths. That would have been more tedious and you can see why I used Stackviews for the example. Good luck.

IOS: Embedding views in stack creating a stack with > 10,000 pts size

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.

How to scale height of views and postion of the views relative to the screen size using AutoLayout [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Using Autolayout what is the best way to keep a view's relative position and scale to each screen size? For example there are drastic differences between the 4s and iPad Pro. What type of constraints and thought processes should I be using?
The views being scaled may each have varying height and take up a different proportions of the screen. I know there are many methods to tackle this but what are the best ways.
Can the answer attach pictures so that I might clearly understand the process?
Thank u very much,
This is a pretty in depth question but to scale a view to the size of the parent and on down you need to use
Equal Width Constraints
Equal Height Constraints
Aspect Ratio Constraints
I use these very often. Here is a quick example
Suppose we want to divide the screen into 3 views. All with equal widths to the main controller view and we want the top to take up maybe 40% the 2nd view to take up 30% and the bottom to take up 20%. We can use equal heights constraints to the superview and edit and adjust the multiplier 0.2=20%.
To set a equal height constraint drag from the view to the parent view in storyboard and let go(you can also do this in the view hierarchy to the left Ex.Picture included).
You will see this menu.
Choose Equal heights and Repeated for equal widths. The width is fine in this example as I want it to be the width of the main view but we need to alter the multiplier on the height or each view will be the height of the main view.
Find the constraint in the size inspector and locate the constraint that says equal heights to superview(image below). Double click or click edit and bring up the menu in the image above. In this case I am setting the multiplier to 0.3 which is 30% of the parent. Now proceed to do this for all the views setting the percentage you desire.
OTHER Examples of Control Dragging to Parent Below:
I pinned all my views together.Meaning the top and bottom constraints are 0 between the views. I hope understanding these types of constraints and this is not an issue.
Example Picture of the pin menu. You can change these to 0 or whatever and quickly add Top,Bottom,Leading and Trailing for Any View
You can also use center horizontally and vertically and you can use a multiplier to keep relative position to parent view as well.
Now as to adding subviews to these views and having them scale proportionately. The process works the same way. See my final 3 big views and I am adding a smaller view to the top view I had just added that is taking up the majority of our screen. I want this new view to be like an avatar in a design.
I put it inside the top view and I am control dragging inside the small view without leaving it to get an option to add aspect ratio(notice I set the width and height equal in the size inspector before I did this step 90x90).
Let go you you will see this menu. This constraint means the view will always be square. It only needs to know its width or height and will solve for the other. Note:make sure the view is the ratio you want before control dragging in storyboard. See next image.
Now I add a top constraint of say 10 and a left of 15 to the view holding this avatar small view using the pin menu. Autolayout is still mad but all I have to do is add a width or a height since the aspect ratio constraint will solve the one we do not provide. I control drag from the small view to it's parent(anywhere outside the small view). I choose equal heights. I locate that in the size inspector to the right and change the multiplier from 1 to maybe 0.4. Now Autolayout is happy again as the small view knows how wide and tall it should be since we gave it a equal heights constraint and it knows that is supposed to stay square it solves its width.
Side Note:
If I don't want to use fixed top, bottom,trailing,leading you can adjust multipliers for align vertically and horizontally to work the same as equal widths and keep relative position.
I update the views and I can continue adding views. Here is what the result would be in Preview.

iOS 9 Auto-Layout and View Controllers; Knowing how to put the right constraints

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 :).

Resources