iOS - How to position an unknown amount of SubViews with different Sizes horizontally centered. - ios

I'm stuck with a problem I'm sure one of you guys can help me with.
I'm developing an iOS-Application with Swift3 and what I'm trying to create is something like this:
Screenshot1
Screenshot2
The screenshots show just a part of my UIViewcontroller. The remaining space is filled with other views that are not part of the question.
As you can see the size and position of my subviews(the icon) depends on the amount of icons i'm given (not more than 8). If more than 5 icons exist it should create a second row and decrease the size of the icons to fit the space. My views should always be centered meaning they are evenly distributd to the left and right.
What i tried so far:
I created a horizontal StackView and tried to fill it programmatically but that did not solve the problem for more than 5 Views.
Tried setting up some views with auto-layout constraints but could not solve the problem of different sizes and positioning.
Of course I could do everything in code. Creating and placing all Views one by one but I would like to find a cleaner solution.
Thanks for you answers. Feedback is appreciated.

UICollectionView Can be a better option here.

Its either UIStackView or UICollectionView since they both handle the distribution of subview. Since you have a max of 8 icons I'd just use UIStackView and vertically stack two horizontal stackViews. You put items 1-5 in stackview 1 and 6-8 in stackview 2. If you need the two rows to have the same distribution then you need to put empty views with horizontal constraints equal to the imageView width to make the second stackview have as many items as the first when the number is odd.

Thanks to Shuja and Josh for leading me in the right direction.
I ended up using a UICollectionView and changing some values inside sizeForItemAt to respond to changing cell sizes and insetForSectionAt functions to center my cells horizontally.

Related

Square Positioning Auto Layout

Whats the best practice to handle constraints for those squares (green and blue ones)?
The four, blue ones, are all of the same size.
I've spent some hours trying to get the constraints right, but everytime it doesn't fit for the IPhoneSE and smaller devices.
How to set the constraints here?
You can achieve what you want the below way:
Use two horizontal stackviews to hold 4 views.
Embed both the stackviews into a vertical stackView.
I am attaching an image for your reference:
Hope this helps. For any queries feel free to leave a comment.

Using Auto-Layout in custom UITableViewCell

I'm working with a custom UIableViewCell that has four UILabels inside of it.
What I'm currently struggling with is that for smaller iPhones, I'm able to get the correct portrait layout (the numbers correspond to the label numbers).
What I'm trying to do for larger iPhones (screenshot below is from a 6SPlus) is have label 3 move in the direction of the arrow:
I have three constraints for the third label. My thought was to have one of them set with a priority of 1000 to be the "base constraint", and another, set at 999, to change the spacing of the cell:
However, it doesn't seem to have any effect. How can I alter the position of the third label using constraints based on the size class? Thanks!
EDIT: This is an approximate illustration of what I'm trying to get to:
Use a UIStackView and put your labels inside it. The stack view can be configured to distribute evenly your four labels, putting the same amount of space between them regardless of how the width of the table grows or shrinks as it launches on a bigger or smaller phone. Thus, you'll look great everywhere.
(If you're not able to use UIStackView, you can achieve the same effect of even distribution using invisible spacer views.)

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.

Split View into four Subviews with 25% width

I want to insert 4 Subviews with exact 25% percent width of the SuperView.
So, my question is how to achieve that with XCode's Autolayout? In Android I would use a GridLayout or a LinearLayout...
Thanks in advance
Just set your constraints like this image,you will get what you want
And this is preview in different screen sizes
You have to select all the four views, and add top, bottom, left, right constraints to zero and equal width constraint.
Your constraints should look like this.
In the screenshot, the four views are arranged over a container view. If you dont't want the container view, then you have to specify the height constraint also.
You should use UIStackView that is introduce in ios9 and xcode 7.0
Here is a link of the tutorial have a look its like a linear layout in android
http://code.tutsplus.com/tutorials/ios-9-getting-started-with-uistackview--cms-24193
There are alot other ways as this is the new one and it help to reduce work and time both so you should use it.Thanks

Xcode 6 auto layout Buttons overlap each other

I'm having a problem with auto layout in XCode 6. I have 2 buttons in a view - the buttons overlap each other. I've tried spacing them out further from each other in storyboard, I've also tried resetting onstraints on them, various combinations of constraints on them, and various constraints on the View Controller. None of this seems to work, they always seem to overlap each other, when I want to have at least some space between horizontally.
I'm attaching a screenshot for reference. Thanks for any advice on this.
There is no way in AutoLayout to set an even number of items to be equally spaced from the center of the superview. You will need to create a clear placeholder view between them, center it in the superview (and give it constraint-based height width), then set each button to be horizontally offset from that.

Resources