Dynamically change axis in UIStackView - ios

I have 2 views in a horizontal UIStackView. What I'm trying to achieve is for the stack view to stay in horizontal mode if possible, but if this isn't possible then change to a vertical mode. Demonstrated in the image below. When in the second case it can't fit horizontally it changes to vertical:
How could this be achieved using UIStackViews and autolayout?
In a more general sense I would love to have a view that mimics CSS flex-wrap properties. I.e.:
Though that seems like a far more complicated task.

Related

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

What is the optimal way to align buttons and text? Basic, but necessary due to misinformation online

I've seen a lot of different topics and suggestions on aligning and inputting buttons/text, but the ways I've seen seem kind of risky.
What is the optimal way, for example, to add two buttons, stack them together, and have them be 10% from the bottom of the screen, and centered horizontally on all devices?
Learn Auto Layout if you haven't yet. Use constraints for achieving the following:
For centrally Horizontal on all devices: Use Center X with SuperView.
For having them 10% from bottom, use multiplier value say 0.10 .
The optimal way would be using storyboard for implementation and use of constraints in a proper way.
For example, as you suggested you want bottom space to be 10% of device height so it can be done with the multiplier in constraints also the horizontal center can be easily done with the same, so you could look for a good tutorial of Auto Layout and constraints for better understanding.
Use your Storyboard and add Auto Layout to your elements. Here is an example:
I have added auto layout
Left
Right
Top
Bottom
If I want to change the distance between the buttons, I just choose the auto layout constraint that I have created between the buttons and changes the constant value to it.
If I say that "Button 4" constant to the top shall be 40, then it´s always 40 no matter what size the phone has. If I change the constant between the buttons to 10 then it´s the same for all sizes.
Your best bet is to use Auto Layout. It takes a bit of learning but once you get used to it you can add constraints pretty quickly and easily in Interface Builder. I can't recommend any particular guide but there are a lot of good ones to be found with a quick Internet search.
Here is an example of constraints that seem to be what you are looking for:
For iOS 9, an even simpler Auto Layout approach would be to use UIStackView.
As you can see, no constraints are needed for the buttons embedded in the stack view, as the stack view lays out the buttons for you. All you have to constrain is the location of the stack view itself.
Here's an example of two vertically stacked buttons, 10% from the bottom of the screen, and centered horizontally for all devices.

Autoresizing UI components using Autolayout iOS8

I have designed a screen for a portrait only iPhone app. I have applied Autolayout so that the view horizontally fits all the screen sizes. The problem is, for smaller screen sizes, the content is truncated vertically. I tried not to set a fix height of components but it breaks all the alignment of the components.
Please see the image for reference;
Any quick suggestion for what I'm missing...
Thanks
If I were you I would would follow one of two approach -
you can use spacer views for spacing between components so that will adjust spacing on the basis of screen height
2.or if your content is bigger than iphone screen then you can always opt for scroll view.(I prefer pure layout approach)
Set vertical spacing between components. Don't fix width and height of components. set aspect ratio constraint for every component.
Like many of the comments and answers above, there are many ways to solve this problem. The decision for you to take is which is more suitable
Aspect Ratio Scaling : One of the common ways to retain the same UI across all form factors is to use the aspect scaling techniques in which constraints are given to mimic the aspect scaling.
Flexible Spacing and Sizing rules : Another way to design the layout is to design the layout to have different types of spacing based on the form-factor of the device. This way layouts may not to be consistent across.
Using Size classes for iOS8 and Above these can be used maintain various layouts for all the form-factors.
Use of ScrollView scrollView can be used if you want to retain the original layout and have scroll to view the layouts that are out of bounds.

Resources