How to correctly configure my stackview? - ios

I drag a vertical UIStackView in my storyboard, I constrain it vertically and horizontally and give it fixed dimensions, 300x300.
Inside this stackview, I put:
a UIImageView (Aspect Fit)
an horizontal stackview containing two buttons
I have no problems if I equally space the content inside the main stackview, but it's not what I want.
I want the imageview to take 70% of the vertical space, leaving the other 30% to the buttons.
I tried using Fill proportionally on the main stackview, but, while in the storyboard is anything ok, when I test on simulator the two buttons totally disappear and I only see the imageview with its content.
What am I doing wrong?

I've recorded a short video
Basically you need to set the height of the image to be equal to 70% of the stackview height. Hope it'd help

Related

Align Image View without stopping a stackView auto-resizing

I am currently facing an issue where I have a horizontal stack view with a UILabel and a ImageView. I am needing the stackView to dynamically change height depending on how many lines of text there is. I am also wanting the imageView to be aligned with the bottom of the UILabel(even when there is a second line).
I have currently tried:
Adding a UIView and aligning the ImageView in it. This stops the stackView auto-resizing.
Changing the Content Mode to bottom doesn't work either as I am wanting aspect fit.
Thanks for any suggestions!
You should be able to get your desired result pretty easily.
Constrain your Stack View to all 4 sides, using however much top/bottom and left/right "padding" you want.
Give your Image View a Width constraint (I'm guessing you want it around 24), and an Aspect Ratio of 1:1. That will keep it from stretching vertically. Content Mode: Aspect Fit
Set your Stack View properties to:
And... you should be all set.

How to keep equal space b/w views on all iOS devices programmatically?

I am getting a very small issue in setting up my screen views equally on all iPhone screen sizes. Attached is the screenshot of the same. I want to keep the spacing (X) equal b/w view's top-view1-view2-bottom on all iPhone devices. What will be the best approach to do so ??
I tried to use view1 & view2 in stackView and distributed them equal vertically. But that way the logo image is also taking full width as of view's frame. I know I am missing very little thing in my code. But I am not able to get it rightly.
Any help would be appreciated. Thanks in advance.
Use Stack Views to place all the elements vertically. May be you will have to use multiple stack views. i.e.
StackView1 would be the textFields and button
StackView2 would be logo and stackView1 with minimum spacing, which is the consistent spacing you want.
Then make your stackView2 centre vertically and horizontally within the container/superview.
Lastly, make stackView2 top, bottom, leading, trailing anchors greater than equal to zero, so that it never goes out of the screen bounds.
Put a two UIView. First is above view1 and second in between view1 and view2. make background color clear for both views. Put equal height constraint for view1 and view2.

Image is not working as expected with constraints

I have a root UIView (for rendering shadow) and it's content is another UIVIew that contains a UIImage and some labels at the bottom. When I look at a sample poster with my constraints set up in interface builder, it looks more or less how I want it.
In this screenshot, the DropshadowUI View holds the content and renders a shadow. The CardView holds both the UIImageView and the UILabels at the bottom. The DropshadowUI View is transparent, the CardView has a blue background and is the full height/width of the dropshadow (w/ a little bit of margin) and the UIImageView has a green background to help distinguish the views while testing. You can see that the UIImageView takes up roughly 80% of the CardView layout, which is desired. The rest of the CardView is made up of the UILabels.
When I run the app however, the constraints don't seem to be working the same way as shown in interface builder. The UIImageView stretches to take the entire height of the CardView. The UILabels are hidden from sight because of this. Judging from the green blur behind my UINavigationBar and UITabBar, it appears that the UIImageView is stretching vertically beyond the extents of the visible area, despite the image not even being that large.
The UIImageView image is set in the interface builder for now (will be set programmatically when this is working). The Content Mode is Aspect Fit. Why does the UIImageView stretch vertically? The Status label has a Bottom Space to bottom of Superview set at 8 points. The Year label's Bottom Space is constrained to the top of Status. The Movie Titles bottom is constrained to the top of Year. Movie Title has it's top constrained to the bottom of the UIImageView. The assumption was that the labels would prevent the image view from growing on the bottom, and instead getting taller by stretching the top (which is constrained to the superview views top.
Is there something wrong with my constraints? I'm still learning the auto-layout system, so if there's something in the screenshots that don't help explain my constraints, let me know what I could do to help clarify them.
Edit
I reset the UIImageView to the suggested constraints and ended up with this at runtime, despite the design-time view looking correct.
Is there a way for me to constrain the UIImageView so that the image can only grow taller, until it runs out of space to fit the rest of the content? I'm missing my bottom label here because now the image is fitting into the UIImageView but pushing everything else to far down. I'd rather it try to fill as much horizontal space as it can, until the siblings beneath it can no longer be pushed further down.
What I get, vs what i want
Select your image and go to Layout Issues tool > Reset to Suggested Constraints

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.

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