Proper Alignment and Distribution selection for Horizontal StackView - ios

Following is the screenshot showing the design I want to implement.
For this, I am using a vertical stackview that encompasses four other horizontal stackviews. Then each horizontal stackview includes an image and a label as indicated below.
I set the alignment of the horizontal stackviews fill and tried changing the Distribution. However, I am not able to get the desired result.
What alignment/distribution combination could generate the desired effect?
Or should I eliminate horizontal stackviews and populate views inside the vertical stackview?

You can accomplish this with a tableView , but if you have a few items then
setting a spacing for the main stackview and every inner stackView should give the needed padding
for Full look check the demo
https://github.com/ShKhan9/stackV

Follow the steps,
1. Add a vertical stackView with top, leading and trailing constraints to the view.
Alignment - Fill
Distribution - Fill
Spacing - 20
2. Add 1 horizontal stackView to the vertical stackView
Alignment - Fill
Distribution - Fill
Spacing - 20
3. Add imageView with height and width constraints set to 30 in the above horizontal stackView.
4. Duplicate the horizontal stackView in vertical stackView the number of times as per your requirement.

Related

How to give dynamic height to labels contained withing stackview in swift

Inside a UItableview Cell I have following structure.
It has two Stackviews Left and Right
1) Left Stackview contains 3 views. Each view containing 1 label (Label1,Label2,Label3)
2) Right Stackview contains 2 views. Each view containing 1 label (Label5,Label6)
Constraints for Label1,Label2 and Label3 are Top,Left,Right and Bottom with respect to it's parent views.
Now when I add text to Label1 , Label2 and Label3 at run time it gives dynamic height but leaves space in top and bottom, for those label having less text. Below is the output.
I want to reduce the top and bottom spaces for all the three labels i.e Label1,Label2,Label3.
Any help will be appreciated.
This may help you
I think You have use distribution of Stack view as fill equally so it gives equal space to each one.
I think for this you have to use fill proportionally. this will adjust height accordingly
Choose fill proportionally from here
if this not working then choose fill proportionally and give height to each label it will adjust.
Check how to give height to labels .
after giving each label height inside stack view like this .
your it will adjust itself according to data.Like this
For dynamic height use self sizing cells in tableview. Set rowHeight and estimatedHeight properties for tableView
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
Set the stackview distribution property to .fillproportionally
as per the UIStackView documentation of .fillproportionally property
A layout where the stack view resizes its arranged views so that they
fill the available space along the stack view’s axis. Views are
resized proportionally based on their intrinsic content size along the
stack view’s axis.
in code:
stackView.distribution = fillproportionally
or in storyboard
Please check the attached screenshot, there are showing fully step by step guide line. How to set UILabel dynamically height inside vertical stackview with multiple UILabel's.
If you have any queries please comment.
Check the attached screenshot

iOS: Can't change a width in a horizontal stackview

I'm starting with iOS app and creating a table view. In my table I have a cell like this.
Now I need to center the horizontal stackview with stars vertically and horizontally. This stackview has fixed width and height.
It have to look like this.
For this I put it in another horizontal stackview. I tried to add the same constraints to it as you see for the label
The problem is that this external stackview does not stretch like a label. I also see that it's width and height are greyed out, I cannot change them. Why?
There are many ways to autolayout this.
I think you should gain a better understanding of StackViews for future usage.
I recommend reading on these resources:
https://developer.apple.com/documentation/uikit/uistackview
https://www.raywenderlich.com/160646/uistackview-tutorial-introducing-stack-views-2
In order to achieve what you need:
Use only 1 horizontal StackView containing the stars. Constraint the stackview to the label by center horizontally.
Use 2 StackViews and keep your constraints. Outer stackview, change it to vertical axis and center alignment.
Method 2 Image in Attributes Inspector (cmd + option + 4):

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

StackView in stackView does not work with its spacing by swift

In UITableViewCell, I create a vertical stackView.
I add horizontal stackView in the vertical stackView(consists of one UILabel(A) and UIImageView) and a UILabel(B).
I set the spacing is 6 points to all stackViews.
But if the width of UILabel(B) is longer than that of UILabel(A) of horizontal one,
the space expends more than 6 points in horizontal stackView.
How can I solve this problem.
In picture,
above one has a problem that I want to solve(See mute icon is arranged to right to the end of UILabel),
and the bottom one is on correct way.
Take Another Horizontal StackView Add UILable(A) and UILable(B).
Set Alignment and Distribution Property Fill.
Give UILable(B) constraint to trailing space right side.
If problem not solved send storyboard screen shot.

Dynamic table view height

The idea is to resize height of the cell automatically based on few controls heights. As you can see on image above I have Top Label and Bottom Label. These two labels can have different height based on text length.
Few words about setup in storyboard.
I set number of lines to 0 for 2 described labels to allow grow their height dynamically based on given text.
For the Top Label I have next constraints:
For the Bottom Label I have next constraints:
So I we say about vertical spacing between 1000 green label and bottom label it's every time the same:
But without this spacing cell won't stretch height. How can I reduce this vertical spacing? Because there is to much spacing between "1000 green label" and bottom label in case if top label have big height because of text.
In -viewDidLoad method I set:
[self.theTableView setEstimatedRowHeight:145];
[self.theTableView setRowHeight:UITableViewAutomaticDimension];
Seems it works pretty cool sometimes with a bug described here but I don't know how to restrict that vertical spacing:
http://www.appcoda.com/self-sizing-cells/
Set the bottom label's top constraint to be >= 11.5 (or whatever its minimum spacing should be).
This will let the cell adjust that vertical spacing, depending on the other content in the cell.
Update:
In iOS 9, this would much more simply be handled by UIStackView.
A horizontal stack view would constrain (and determine the cell height based on the) two inner vertical stack views. The left vertical stack would handle the image, banner, and label layout, and the right vertical stack would handle the top label, 10000, and bottom label layout. You'd only need with 4 constraints (for the horizontal stackView to constrain it to the contentView).

Resources