Xcode - Vertical Stack of Horizontal Stacks constraints - ios

I'm very new to using Interface Builder and Auto Layout so please bear with me.
I'm trying to lay out a vertical stack of horizontal stacks, with each horizontal stack being made up of a label and another horizontal stack of two labels. The whole vertical stack is wrapped in another view that has 10 points left and right constraints. I want the two stack elements of each horizontal stack to be at the edges of the stack.
I can get the layout to look how I want it on the interface builder but when I run it, the Sunrise AM/PM label gets cut off.
Storyboard next to simulator instance where AM label is truncated
View hierarchy with constraints
Please let me know if there's anything unclear about what my goal or my issue is.

The Problem is that your programm does need to truncate something to fit everything on the screen. Since you probably did not define which element to truncate (by altering the compression resistance) it takes the last one in the stack - the am/pm one. Changing the compression resistance should fix this.
Check out this guide to learn about it.

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:

iOS - how to arrange multiple components vertically using AutoLayout?

I have design the screen like below image
I solved this method..
set Y position of RedView,
set vertical spacing of GreenView,
set vertical spacing of BlueView...
But this method seems inefficient.
Need help to achieve following Task.
Components should always be stacked based on the center regardless of number. (because the number of components can change by requirements of client.)
Components have various height.
Is there a good way?
Use Stack View Buddy .. It will make your life a lot easier ..
That will give you an idea how to use them
https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views
Also go through the API Reference .. It will be helpful
https://developer.apple.com/reference/uikit/uistackview
They will automatically adjust themselves on item size changed or screen orientation changed ..
You need to drag your components from the object library and put them vertically into your view. Then select all the components and stack them vertically, using command: Editor -> Embed in -> Stack View or by clicking the stack view icon at the bottom of the screen next to the alignment icon.
Then centre the stack view vertically and horizontally in the view.
Stack views have a number of options that allow you to distribute your components within a stack view: use alignment and distribution options in the attributes inspector to achieve proper positioning.
For more advanced tips, read about autolayout and stack views on raywenderlich as suggested by Umer Farooq above.

How to have Auto Layout set up a grid of icons proportionally for each device

I am coming across a few issues with Auto Layout when I set up my storyboard.
I want my app display to be identical (image size, spacing, proportions) on each generation of iPhone.
I have somewhat achieved the results that I want by setting each UIImageView to have central Autoresizing and an Aspect Fit Content Mode; however I cannot get the same results once I place the UIImageViews into Stack Views which I have been lead to believe is the proper way to set up this 4x4 icon grid.
Any help is greatly appreciated and if any clarification is needed please let me know.
Stack Views are here to simplify things.
You need 4 horizontal Stack Views inside 1 vertical Stack View.
Then, you need only 4 constraints in the vertical (main) Stack View: Left, Right, Bottom and Aspect Ratio (1:1).
The Stack View configurations are all the same: Fill and Fill Equally.
In my example I also added a space of 10 to each Stack View.
Like this:
Bonus: notice that I'm not using UIImageViews, but custom UIViews,
which draw themselves at runtime. You can even live-preview then in
Xcode via #IBDesignable and #IBInspectable.
Results
iPhone SE:
iPhone 6:
iPhone 7 Plus:
Take a look at the code:
git clone git#github.com:backslash-f/grid-on-stackview.git
(Please give Stack Views a chance. :-))
If the above gives you difficulty, which will not be the case, you can use a container view AutoLayout pinned to the 3 edges. Then place a https://developer.apple.com/reference/uikit/uicollectionview in the container view and set up the collection view with your data source. This is how I solved this problem in the past. Be sure to shut off the scroll, bounce, and control the amount of data source items to perfectly fit the numbers of the grid.

How to arrange UIStackViews?

I'm starting a new project that supports iOS9 upwards and after looking at Apple's constraint guidelines they appear to suggest using StackViews whenever possible. After reading a few articles and the apple documentation I've a basic understanding of how to create them and their benefits but I'm still not sure when not to use them and how to arrange them.
For example in the below view should I use:
One big StackView on a vertical axis that covers the entire super view.
Three StackViews with regular constraints pinning them to each other and the super view.
One big StackView that covers the entire super view with three stack views within that view
No StackViews, this view isn't suitable
In general how do I decide how I layout my stackviews and whether to use them?
I've started to use stack views more and more, especially since Xcode 8.x. Every stack view you add saves you adding some auto layout constraints (3 vertically stacked labels in a view would probably need 9 constraints, that could be just 3 with a stack view)
If all elements are in vertical stack views, it's unlikely you'd need to embed one inside another - you'd usually do that when you have a horizontal one inside a vertical, or vice-versa. So in the example above, I'd start with one large stack view.
In Xcode 7.x there were issues with the intrinsic sizes of UILabels not being calculated correctly. In these cases, you can set a placeholder intrinsic size for each label in the size inspector.
That problem aside, get stacking!
I have a problem and can't see your screenshot but I have some points that help you decide:
Do use stack views for all linear arranged views
I prefer set the root stack view to the size that contains exactly the content without whitespace (so constraint it to be as the superview size only if that's the content size)
The stack view uses auto layout to determine the size of it's subviews, so you should validate that your subviews do tell their best suitable size - maybe by using intrinsicContentSize() [only when needed!] (be careful with it)
you can practice stack views in interface builder, try to change stack view properties, hide subviews (with hidden property), and play with constraints, it's great!
Good luck =]
Have a nice play

Can't get Auto Layout and Stack Views to Auto-Adjust Properly (Swift 3, Xcode 8)

I'm new to iOS development so naturally I'm having some issues with my stack views and auto layout constraints in Xcode. Originally I had used just constraints and the pin menu to align everything but I'm really trying to get stack views down so I went back and implemented them.
If you look at my images above you'll see that in my app I created a couple of stack views:
Status Bar vertical stack
Section 1 vertical stack
1st divider (which is just a view with a height of 0.5 and is not inside of a stack)
Section 2 which contains 2 horizontal stacks inside of the main vertical stack view
2nd divider (exact same thing as 1st divider just between Sections 2 & 3)
Section 3 contains a vertical stack and horizontal stack inside of the main vertical stack
And all of these stacks plus the 2 dividers are grouped together in 1 super vertical stack called User Interface.
One of my problems is that I can't set top and bottom constraints between my stacks and dividers. As it stands, there is too much space for my liking between the sections and dividers. If you look at image 1 I tried to set a top constraint of 15 between Section 1 and Status bar. I also tried to set a top constraint of 15 between my 1st divider and Section 1, and so on and so forth. I'm getting conflicting constraints and I just can't figure out why. All they say is Section1.top = Status Bar.bottom + 15 , 1st Divider.top = Section1.bottom + 15, etc.
Any ideas on how to resolve the conflicts? Every time I try to move a divider it snaps back in its original place and adjusting the constraint numbers give me the same errors.
My second issue is that I can't get User Interface to fit inside of it's super view. I want my app to fit in all iPhone screen sizes and to auto adjust accordingly. What I tried doing is using the pin menu and pinning each side of my User Interface stack with a constraint of 0 (Constrain to margins unchecked). User Interface is aligned and without any conflicts but then all of my stacks get squished as seen in image 2. This results in a couple of errors saying that some of the heights and vertical positions of my labels are ambiguous:
Height and vertical position are ambiguous for "Bill Amount Text Field".
Height is ambiguous for "BILL AMOUNT".
Height and vertical position are ambiguous for "Tip Percent Segment Control".
As you go down the screen sizes there are more errors as more labels are being squished. And I already set individual top and bottom constraints for each label. Even when I implement a set height for each label, the error messages go away but not all of my labels appear within their stack. I tried messing around with the Alignment and Distribution menus and selecting different ones for my stacks but none of them seem to fix the issue.
Any help would be greatly appreciated or if I'm going about this all wrong please let me know. I'm using Xcode 8 beta 4 and wrote the app in Swift 3.0.
Refer to this documentation - there are pretty clear explanation of all auto-layout concepts.
I presume that your problem is that you use one big stack view, where each element fills proportionally to others. Try to fix this out.
One little advice, stacks are really good thing, but you should not abuse it :)
TL;DR;
From my experience. Stop using Interface Builder and create all views in code. With new anchor system or frameworks like SnapKit it's very easy. The benefits you get:
Faster development (after some time of practicing)
No stupid warnings from Interface Builder
Easier to merge when you work in a team
XCode almost never crashes
I was fighting with IB for few months and now I'm totally happy.
Good luck.

Resources