Center UIImage vertically in Stack View - ios

I've a stackview with an image and another stackview. This sub stackview contains two labels, the username and the tweet text. The tweet text may contain multiple lines.
I'd like the image to be centered vertically within the stackview. If I add a vertically align constraint to the UIImage the stackview takes the height of the image and truncates the twitter text.
Is it possible to vertically align the fixed size image vertically in the stackview without the tweet body being truncated with auto-layout? Or has this be done programmatically?
Cell
Constraints

Try setting stack.alignment = .center, if the view is in stack, you should modify the stack properties, and not the UIView's.

Related

Auto size horizontal UIStackView width based on variable number of buttons?

I have a UIStackView in which I programmatically add buttons based on the available actions for some content. I'd like the stackview to grow horizontally as each button is added and each button to be only the width needed to contain the text.
Where I'm at now, all buttons are sized to their text in the stackview and the first button takes the remainder of the stackview width as layed out.
What I'd like to do is have the stackview size in width dynamically based on the buttons added and how wide each button is.
This is what I have now:
Horizontal stackview with three buttons
Give your stack view these properties:
Constrain the Top of the stack view, and constrain it Centered Horizontally:
As you add buttons, its width will expand to fit the buttons, and it will remain horizontally centered:
Edit
And, if you want the stack view to expand left-to-right, constrain the stack view's Leading instead of centered:

iOS How to fix UIButton height in a custom UITableView cell

I have a custom table view cell with two buttons. The accompanying text has a varying number of lines which affects the height of the cell. This height variations causes the height of the buttons vary. I have tried a lot different things to fix this with no luck. Currently the two buttons are in a stack view and that is in another stack view with the UILabel. I'd like the buttons with a fixed height vertically centered in the cell. What's the best way to do that?
Use only one stack view - for the buttons.
Constrain your UIStackView to the trailing edge of the cell's content view, and constrain it centered Vertically.
Constrain your UILabel to the leading edge of the cell's content view, and constrain it centered Vertically.
Also constrain your UILabel horizontal-spacing to the UIStackView, and edit it to be >= 8.
And, the key part, also constrain your UILabel Top and Bottom to >= 4.
Now, when your label wraps to multiple rows, it will "push the top and bottom", and your stack view holding your buttons will remain vertically centered, but will not "grow in height".

Overlap label on imageview inside uistackview ios

I would like to overlap two views (one view on top of each other) inside uistackview . My objective is to display initials of username on top of his image. The image size is constant and the initial label should be in centre of image view
I have a uitableviewcell on which other labels and buttons are also organized
here is the view hierarchy.
I tried to fix this in following way and it works
Excluded the view which I wanted to overlap from UIStackView
Make that Label to align with following constraints
Center Vertically with ImageView
Center Horizontally with ImageView
Equal Widhts with ImageView
Equal Heights with ImageView
To overlap views use -ve spacing in StackView

Stackview arrange issue

I have a Horizontal StackView inside vertical StackView.
Horizontal StackView contains a image (w=30 , h= 30) and a label.
Need
I need to make image & label to center horizontally as per label intrinsic content size.
So, it looks like ----Space----- image & label -----Space---
How can I achieve that?
Thank you,
i am using only horizontal stackView to make it center.
What i did.
Step 1:
make my image with specific width and height.
Step 2:
Both are inserted in Horizontal stackView and make that stackView center aligned.
Edited:
If i use vertical stack View On top, i can't find any problem for centering label and image
You can use UIView inside Horizontal StackView then add image and label on UIView
Please take a look on attached image
Because the VerticalStackView forces the HorizontalStackView to have same width, So you have to wrap imageView and label inside a UIView. Like this
Add two extra views, I named it View 1 and View 2(green color) on left and right of your image and label, set two views width equal so your imageView and label will automatically center, don't forget set width for imageView and label. Set view 1 and view 2 clear color or under other view view if needed(to see background, other views, interaction with other views...).

Constraints are not working at Buttons with Images Xcode

today i have set up some constraints in a view but there looks awful. Which Constraints have i to add (see screenshots)
In XCode
In Simulator
If I have understood your requirement right, you need to have four images with labels below these individual images. If this is the case, you can use collection view whose cell contains a image view and a label. You can give the spacing between cells, size of cell and intercell spacing through delegates. Also just you need to specify image view and label constraints in collection view cell. Like for instance, image view placed horizontally centre and pinned to top of superview, label placed horizontally pinned to bottom of image view and top of label to give a gap between them.
Fix the all images height and width, and given the leading, trailing, top and vertical spacing between label and images. Also fix the label height. You will given the min 4 and max 6 constraints for every fields.

Resources