StackView in stackView does not work with its spacing by swift - ios

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.

Related

Constraint for StackView in Xcode so the buttons stay always to the center of the view vertically?

So i created a simple UIView. I added two UIButtons. I have added them in a StackView. I have put the Alignment of the stackview to fill in xcode attributes inspector and the distribution to fill equally and the spacing to 50. Then i added a height constraint of 300 to the stackview and i added a custom font as well to the buttons as well as three constraints. One for the stackview bottom as you see at the screenshot, one for the trailing and one for the leading constraint. I haven't put a top constraint yet. When i change to different devices through xcode the buttons are not centered. I added the top constraint but i guess because of the height constraint at the stackview there is an issue and all constraint become red when i change to another device than the initial i created the stackview. If i move for example to Iphone SE the buttons move to the top side i guess because of the bottom constraint... So is there a way to center the stackview with the buttons as i change to different devices? Any help appreciated.
For a stack view to be centered you needs leading, trailing and vertical centering constraints, stack view will have automatic height based on buttons heights and vertical spacing, no need for bottom constraint

Proper Alignment and Distribution selection for Horizontal StackView

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.

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

Auto-layout issue

I have a view with two button. I have added auto-layout. But bottom space is more in iPhone 8 plus than iPhone 4s.I want to keep the spacing ratio of top and bottom same in all iPhone. How to fix it ?
Updated
For Specific Ratio : To my understood, We can give Multiplier for Centre Vertically Constraints. Here ratio of top and bottom space will be same in all iPhone series. I gave Multiplier as 1.4
==================================
I know having several answers there. But, no one used UIStackView to add two simple buttons.
Drag two UIButton, select that two UIButtons, click Embed in Stack icon from here.
Stackview Properties
Give following things for UIStackView,
Set Axis as Horizontal, Distribution as Fill Equally, Spacing as 10 in Attributes Inspector
Stackview Constraints 1
Give constraints for that UIStackView, Check/Uncheck Margins, Left and Right = 0, Height as 30.
Stackview Constraints 2
Still, some constraints are not satisfied. Press Control and hold it, Click StackView and Drag to SuperView, you will get black pop up. Choose Center Vertically in container.
Stackview Constraints Customization
If you want to change height of UIButton or Bottom Space, click UIStackView and in Size Inspector, you can change height or move up/down.
If you are using Auto layout then set constraint like this way
set constraints to bottom and leave top constraints
1) Select both buttons
2) Click on "Resolve auto-layout issues" button on bottom and click on "Reset to selected Constraints"
You should have 2 stack views. One Horizontal that contains the 2 buttons, and another vertical that contains the previous stack view. Set the horizontal stack view to "fill" and "equal spacing" and the vertical stack view to "fill" and "equal centering." Below I've included a couple of pictures to illustrate what I mean.
THE ONLY CONSTRAINTS ARE ON THE STACK VIEWS THAT BIND THEM TO THE SIZE OF THE CONTAINER VIEW, OR PARENT STACK VIEW.
This allows you to truly create an auto layout that scales with the device's screen size.
Use Constraints like this. These buttons have bottom fix with superview.

iOS UITableView cell fit to screen width

I'm new to iOS and trying TableView for the first time.
I added a prototype cell into the TableView. It has label a inside it which is on the right side. When i run it on lower display like 4S, the label is not visible.
I tried setting Horizontal Space - Content View to label (on right side), but has no effect.
I been playing with the auto layout for sometime, but can't figure it out, can someone be kind enough to help me?
Edit:
Before answering straight, I would say stick to autolayouts and not look for frames, springs and structs.
Now pin UILabel. Select UILabel Go to Editor>pin leading and top space to superview. Also pin the width and height (fix them if you want to). Your problem is solved.
Go through this book for autolayouts and keep playing.- iOS Auto Layout Demystified, 2nd Edition
Updated after question edit :
For Label1 set :
Pin Leading, Top and Bottom Space to superview.
Pin the height(not the width)
For Label1 set :
Pin Trailing, Top and Bottom Space to superview.
Pin the height(not the width) again.
Now select both the labels and go and pin horizontal spacing between them. And if meanwhile you update frame... do update constraint also.
give vertical constraints to your label, if there is not that much need of autolayout you can disable autolayout.
The constraint is not enough to determine the vertical position of the label.
Two optional ways:
Add the Vertical Center in container constraint
Add a height constraint of the label and a top spacing to containe constraint (the contentView of the UITableViewCell your case)
With each of the above and your already exist constraint, the position of the UILabel can be determined exactly.
You can add leading, trailing, top, and height constraints on your label.

Resources