UIStackView : Change UIStackView image width using storyboard - ios

The issue : Image take most of the stack and text shrinks
I tried to change content hugging priority but no changes !
How to do it in UIStackview using storyboard?
What I want the cell to look like Eventbright event Cell?
What I got is an image that take most of the cell?

Instead of Fill, change the Distribution of the UIStackView to Fill Proportionally and add constraints for the width of each element. Those constraints will serve as a guide to proportions on one element to each other and not as fixed sizes.

I tried it the following way.
The overall set up is given in pic:
At run time result is as given:
First Stack view is fill proportionally
imageview width is 1:4 of stackview width (control and drag from imageview to stackview and set equal width to 25% )
From control and drag select equal width. as shown
stack view 2 is fill equally
stack view 3 is fill equally
Hope it can help

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

Xcode How to create a square button(width equal to height) inside a Horizontal stackview

I have a requirement to create 3 square buttons at bottom of the page. All of them must be aligned and their size should be square.
So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60.
How can I create square buttons, since the stackview subview width is dynamic and determined at run time?
How can I set an equal aspect ratio for these three buttons?
Don't set the stack view height; let the content determine that.
You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed.
Put the buttons in a horizontal stack view
Constrain the leading, trailing and bottom edges of the stack view to the superview
Set the stack view's alignment to "Fill" and distribution to "equal spacing"
On each button, create an aspect ratio constraint between its width and its height of 1:1
If you want a minimum height for the buttons, set a >= height constraint on one of them
If you want a fixed height for the buttons, set a = height constraint on one of them
I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. Set it to distribute equally as shown in the image below:
To set the height, you simply need to set a height contraint on each button. Click the tie fighter looking icon and set the height for each:

How to center a Label with an ImageView next to it in Swift?

I am trying to have a UIImageView next to a UILabel with dynamic content. Both within a StackView.
Currently I'm using the Fill Alignment and Distribution. Changing it to one of the other options, changes the size of the ImageView (fixed Constraints to 30x30).The StackView has left and right leading Constraints of 0.
This is my current:
The is what I want:
How do I need to set up the StackView to archive my goals? Help is very appreciated.
You can try using a vertical stack view with an embedded horizontal stack view.
The first stack view has a vertical axis. Its alignment is centre and the distribution is fill (the default). This stack view has the following constraints: Trailing to superview, leading to superview and top space to top layout guide.
The second stack view contains the image view and label. Its axis is horizontal. The fill alignment and distributions are set to their default values. The spacing is set to 10 (can be changed to suit your needs). This stack view has no constraints.
The image view has a height and width constraints of 30 respectively.
The label has no constraints.
Both the image view and label have default content hugging and compression values.
If the label has longer text then then it will appear like this:
Have you tried manipulating content hugging and content compression resistance priority ? I believe these property will let you manage the length distance between both the image view and textView, and the stack view won't let you dynamically expend the textView as you enter more and more data so I think manipulating the hugging and resistance properties can give you a solution close to the required.
This can be achieved by creating a container view for the image view and the text label and then centering the container view in the original parent view.

iOS : Size classes issue

I'm creating a UI using size classes. As you can see in the below picture , there's issue with iPhone 6 Plus . Please help iPhone 4s always creates a mess. Do tell me if the UI is correct?
Image description:
Hope this helps!!
Select Login Merchant button, Blue and Pink button with OR label and put them in Stack View with Vertical axis. name it as (Bottom StackView)
Put some proper spacing between them.
If your image height is not fixed then make it relative to the device height, that means create a height constraint of imageView = (1/3)height of mainView.
Also you can take Label and textField Username in Horizontal StackView then combine both textFiled (username and pass) and Label in Vertical StackView.
Make vertical constraints Greater Than Equal to 20 between Bottom StackView and Center StackView.
this will give to better result in all screens.
In Case if you don't use StackView then,
Combine Bottom 3 button in SingleView (BottomView) and Upper textFields and Labels in SingleView (CenterView).
Then set vertical spacing s Greater Than Equal to 20 between them.
Make your image height as relative to screen size as described above.

Autolayout Issue with Multiple Button Aligned Horizontally & Vertically with a Label

Please look at the ScreenShot attached for wCompact|hRegular for different screens, I am trying to make it working since hours but not getting any success. My requirement is that at the top there would be a label with some predefined margin. Although the Label content would render at the runtime, but I know the content size, so resizable label isn't needed actually I think. Now there would be three row at equal distance. In first and third row, there would be two buttons with equal height and width and in second row there would be button aligned horizontally. I have set the buttons image and text in storyboard. Control Alignment are set to Horizontal | vertical. Constraints for label are:
Pinned top space to superview, leading and trailing space equals to:8(superview), height equals:90.
Constraints for Button(View Transactions) are:
top space to label, bottom space equals to:8(New Launches Button) leading and trailing space equals to:8(superview) and 8(Place Request Button) respectively, and equal width and height for all buttons.
Looks fine for 4.7 and 5.5 But not satisfied with the output for smaller screens. As u can see resizing of buttons image not working properly(Larger space between button's image and text). One more thing is I don't wanna set the height of the label, cause it seems like a wrong practice in AutoLayout. Any help would be much appreciated.
You should set the Aspect Ratio for the Buttons, not just the equal width and height. In that case auto layout wont shrink the images.
Really you should put this into a scroll view so that if the height is too great the user can still see everything by scrolling. You should also not set static heights on labels, you should allow the intrinsic content size to apply.
Add a subview to your new scroll view. Pin the width of this subview to the width of the scroll view. Do not pin the height.
Add all of your buttons and labels to this new subview. Pin them to the edges of the view and allow the intrinsic content sizes to apply limits. Set various items to have equal widths and heights. Do not set explicit heights or widths (do everything by proportion or equality so auto layout can choose good sizes).

Resources