Swift - How to make a StackView 100% width - swift

Im trying to create a profile screen for an App. The only element that i cant get the width set dynamically is a 3 x 1 button grid in a stackview.
You can see in the second screenshot how it resizes rather than dynamically resize like every other element

This is my approach. I made stack view which trailing and leading constraints are 0 and image height is set to 30 with aspect ratio 1:1 (this can be customized). Each image has its own View where its centered and then stack view aligment is set to center and distribution to fill equally. This can be used for buttons too.

Related

Setting Buttons in Head of view

I am working with an app for macOS. So I want to set six Buttons in top of view using the complete width of view. When I resize the view the buttons should resize at the same time, so the width of the buttons should raise as the view is raising in same relation
How can I do that. Is it possible using the autolayout? Or do I need to change the position (x, y) and size programmatically? But how can I change the position?
Thank you for your help
Best regards
Programmatically, position and the size can be set by adjusting the .frame property of the NSView object.
https://developer.apple.com/documentation/appkit/nsview/1483713-frame
Also, it could be archived with Auto Layout by setting the left and right constrains of each element (and not specifying a width constrain) so the distance between each element, and the distance from the left-most element to the view.minX and right-most element to view.maxX are fixed (while the width is calculated dynamically). Additionally, add a height constrain so the heights remain the same.
Please follow up if you need more information ;)

Trying to set a wide image in UIScrollview, with a fixed height

Novice that has been stuck on this problem for 2 days (please help!). I have an image that is much wider than the screen. I want the user to be able to scroll horizontally on the Image, and also want the image to be a specific height while maintaining it's aspect ratio.
Currently the UIImageView is nested within the ScrollView. The ScrollView has constraints to the Safe Area in all 4 directions, that place it in the upper half of the screen (250 from the bottom of the Safe Area). The ImageView has all 4 constraints of 0 to the "Content Layout Guide" of the Scrollview, and a 5th constraint of having an equal height to the "Frame Layout Guide" of the ScrollView.
I initially tried to set the image Content Mode to "Aspect Fit". This maintained the aspect ratio and set the image with the desired height within the ScrollView, however left a huge amount of transparency either side of the image so the user has to scroll for a while before seeing the image. For this I could not find a solution that contained the scrollview within the bounds of the non-transparent section only, so I then changed the image Content Mode to "Aspect Fill".
This removed the transparency either side but the image is now too tall and goes off the screen vertically. I have set "Clip to bounds" on the ImageView Size Inspector and also set it to true in code, but the image is still to tall and outside the height constraint specified. I also used:
gym_imageView.sizeToFit()
scrollView.contentSize = gym_imageView.frame.size
But none of this is fixing it.
My expected result was that the image with original dimensions (W: 12064px, H: 1696px)
Would resize itself to the constraint height (0 to top & bottom of Content Layout Guide of Scrollview, which in turn is constraint 0 to top of Safe Area, 250+ to bottom of Safe Area) while maintaining aspect ratio.
Any ideas?
The Content Mode changes how the image appears in the imageView, but it does nothing to resize the imageView itself.
You should set an explicit aspect ratio constraint on your imageView with a multiplier of 12064:1696. Then Auto Layout will be able to calculate the correct width of your imageView based upon the fact that it already knows the desired height.

Trying to resize my UIView containers to fit smaller iPhone screen sizes

I am trying to have my layout fit on multiple iPhone screen sizes, but I am having trouble resizing my UIView's to resize themselves when on screen sizes smaller than the iPhone xr.
I have constrained the views, labels, and buttons properly and everything is aligned, but when viewing the app on smaller screen size, I don't fully understand how to resize my UIView containers to resize themselves to fit smaller screens.
On smaller screens, my button does not show up because the screen size is too small. The UIView's are the issue and the stack views inside them also need to resize or shrink the text to fit everything on the screen.
Image of my storyboard, and respective screen sizes
"I don't fully understand how to resize my UIView containers to resize themselves to fit smaller screens." -UIViews won't resize according to the screen, you will have to add constraint for them so that they pick their width and height. Alternatively you have to set constraint for fixed height and width.
In my opinion, for supporting smaller screen size, you must use scrollviews as parentview. So that user can scroll in the app. Also you can give relative width and height for views inside scrollview.
Scrollviews are required because you will always want some minimum width and height for buttons, labels etc. Otherwise on larger screen like Ipad they will be very large, while on smaller screens they will be very small.
You can use TPKeyboardAvoidingScrollView: How to use TPKeyboardAvoidingScrollView, or just google for that. It handles keyboard showing and hiding task, which is a headache otherwise.
Here is how I think you can solve your issue:
Set Some minimum height and width for your topmost view(scrollview preferably).
Now add other views inside it and use relative width and height. In relative width and height you give values in ratios. Here you can get an idea how to do that: -Giving width in % values in autolayout.
Also add additional constraint on your internal views for minimum height and width so they don't fall below certain size.
I would not suggest, but you may always use UIScreen.main.bounds.size.width and UIScreen.main.bounds.size.height to get the width and height of screen. And according set values for your constraints in your view/controller class. Here is a link for setting value of constraint in swift class:
set contraint value programatically

UIStackView in storyboard forces intrinsic content size

In storyboard I have added a UIStackView to a UIView and inside the UIStackView I've added a UIImageView with an image specified. The image specified is in XCAssets and has a 2X size of 59X60. The UIImageView is getting its width set to this intrinsic size but I want it to be 44X44 and the image to scale to fit. However in Storyboard with and Height settings are disabled and set to 59X60 for the ImageView and for the UIStackView as well. I want to have 5 of these same ImageViews with stars stack horizontally at 44X44 inside the UIStackView.
Ideas?
Update: It seems that adding a height constraint to the UIStackView will cause the scaling but the star is then distorted in appearance.
The width and height fields are grayed out because the image view is inside a stack view. You must use constraints to control the size of a stack view's arranged subviews.
First, make sure all five star views have “Content Mode” set to “Aspect Fit”:
Then, constrain all five star views to have equal width and height to each other:
Finally, constrain one of the star views to have a constant width of 44 and a constant height of 44:
If you want space between the stars, set the “Spacing” of the stack view:
If you want padding around the stars, you can do that outside the stack view, or you can set the stack view's “Layout Margins” to fixed constants (and turn off “Safe Area Relative Margins”):

How to set constraints to multiple UIImageView's in Swift

After hours of attempting to find a solution, as well as trying and failing myself I decided to ask here.
As shown on the picture below I have 15 Image views in a pyramid shape that I would like to set constraints on.
I have tried to:
Constraint equal width and height on all images
Constraint center image on line 1, 3 and 5 to horizontal center
Align vertical centers to all images on each line
Etc etc
The result is always the image view going out of bounds or similar issues. (Clip to Bounds is true)
Anyone have a solution or suggestions?
try putting them all in stack views then putting all those stack views inside a stack view and putting constraints on that
like this take a look at the view hierarchy on the left
to get the spacing i achieved just adjust the spacing on the right inside the attributes inspector
and its even able to have constraints on the highest stack view
all the errors go away once you add constraints to the top stack view
like shown here
Embed all your imageViews in a UIView.
set the top, bottom, leading and trailing constraint inside that view (top image to the superview, etc.).
align the UIView horizontally and vertically in the container.
You probably also want to set a width to your UIView, but I let you think about the final result you want.