StackView inside UIView - auto-height - uiview

It's possible to make an UIView adjust automatically his height to accommodate an StackView, via Interface Builder? I' wanna put a background on a StackView, but as long it is not possible, as StackView is not renderable, I'm using it inside a UIView, but XCode is complaining about a height constraint.
Thanks a lot]1

You need to constraint top, bottom, leading, trailing of the UIView to the UIStackView, and nothing more.

Well, sometimes the answer is so obvious that we can't see it. I've just put a height constraint of the UIView to the height of the StackView and it worked like a charm!

Related

UITextView not visible on UIScrollView

Ok, so what I have is a UIScrollView that is constrained to all four sides of the main view, centered both vertically and horizontally, and set to have equal width and height to the view. All of the subviews that I put on top of the UIScrollView are showing up when I run the app, exactly where I want them to be, but only the UITextView at the bottom is not. It seems like I've tried every combination of constraints but it never appears when I run the app regardless of what I do. Here is a screenshot of the constraints in the interface builder:
And even when I preview the file Main.storyboard before running it looks like this:
But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:
Any help with this problem will be greatly appreciated! Thanks in advance.
Avoid putting all your subviews directly inside the scrollView. The autolayout will break apart.
You need to :
Add a UIView inside the UIScrollView, with constraints 0-0-0-0 to leading-top-bottom-trailing to the UIScrollView, and put all your subViews inside that UIView.
After that, you need to set the contentSize of your UIScrollView by code.
Also, you can:
add missing constraints to see what is missing.
The left panel in UIStoryBoard (Document Outline), you can press the red arrow showing up to see what is missing or conflicting.
Check out Apple documentation for more details.
If you want your scrollView only scrolls vertically you shouldn't set its height equal to its superView so remove it and just set the width to its superView and then it should calculate the height based on the subViews inside it
I offer you to drag a UIView in your scrollView and set the constraints to its four sides, and name it containerView , then set its width equal to background view and start laying out your views inside it not inside the scrollview :)
If Height of all views in the scrollView is clear, it can infer the scrollView's height
in this case you can set a fixed height to your containerView like 800 to get rid of the red lines and check how it works :)
I have noticed some clues you might have to solve which could probably solve the issue. Since the scrollView is extend by its content's elements, you have to explicitly deal with each element in the scrollView:
For imageView on the top is not well constrained, you need to give it a width or aspect ratio. Fix the imageView issue might solve textview height problem, even it's not still is a good start.
TextView bottom anchor is equal to scrollView bottom anchor, but you have to know textView is also kind of scrollView. So it's not reasonable to constraint each other at same time. Because both of them don't have explicitly height. You can try to type some words in the textView which will at least give it height by its content, then the scrollView can detect the textView bottomAnchor. You might see something then.
Your scrollView's height is equal to view's height is also weird, scrollView shouldn't constraint its height at first. Because it can't be "Scroll"View anymore because it's height is constant. you should let its width equal to view, and let the height be decided by its element's height. Then it will be literally a scrollview.
Hope it helps

iOS/Swift: Dynamically size UIStackView when one of its items gets larger

I have two items in a vertical UIStackView: a UILabel and a UITableView. When a dynamic amount of UITableViewCells get added to the UITableView at runtime, the UIStackView does not get larger.
Is there a general way to increase the size of the UIStackView?
You should call sizeToFit() and layoutIfNeeded() on the subviews of the stackView. Constrain the UIStackView as you normally would, and constrain the subviews as you normally would.
Also, you need to have it set to Fill Proportionally and it will resize to fit the new content.
Make sure you don't have a bottom constraint of a height constraint on the stackview. You should only need left, right and top constraint.
I had this on iOS 12, while it was working fine on iOS 13.
Just set:
stackView.distribution = .fillProportionally
Fixed it :)
you one need to fill proportionally and it will work according to your code

UITableViewCell with two UIStackViews

I'm facing a layout issue with a UITableViewCell which consists (from top to bottom) of (1)a UIStackView, (2)a UIView with a fixed height and (3)another UIStackView. In the top UIStackView I want to place a few instances of UILabel vertically, depending on some datastructure. These labels can vary in height. In the bottom UIStackView I want to place a few instances of UIButton vertically. This number also depends on some data structure.
The problem has to do with AutoLayout (at least I think it does). How would I set up the constraints in such a way, that the UITableViewCell creates both enough space for the top UIStackView and the bottom UIStackView when it needs to be placed on screen. The layout of the cell is defined in a separate XIB file.
So far I have a heightconstraint on the top UIStackView. Depending on the frameheight of the labels the height of the stackview is adjusted. But it turns out, that the height always has the same value. I know this, because I need two instances of this particular UITableViewCell in my TableView.
The bottom UIStackView does NOT have a heightconstraint and has always a correct layout.
Do I need to adjust the properties for vertical hugging/compression resistance priority?
Any help is greatly appreciated.
Maybe I misunderstood your issue, but why are you fixing the height of the top StackView if it's height is supposed to be dynamic ? You should remove this constraint and set intrinsic sizes (below "Content compression resistance priority") of both StackView to "placeholder" to keep IB happy.
The top StackView will then behave the same as the bottom one, provided you don't have other constraints issues.

StackView not stretching, hugging content

i got an issue with a UIStackView that won't stretch to the superviews bounds.
i've added constraints to the stackView to fill it's superview with a left and right margin. But instead of doing so it resizes it to it's content size. I've set the distribution to .equalSpacing and axis to .horizontal.
In a test project with just the stackView it works fine (despite the fact that in my actual project i'm doing it in code and in the test project in IB).
Here the constraints of the content views get correctly ignored and the stackView takes the space of the superview
i tried changing contentHugging for both contentViews and stackView but nothing changed. Any help is really appreciated :)
i found it myself, problem was that the stackView is inside a UIScrollView to make it scrollable once the stackView extends the screen bounds. As adding constraints to a subview of a scrollView does relate to the contentSize of the scrollView not the frame, the constraints could not be resolved correctly.
I ended up adding constraints to the superview in didMoveToSuperview()
Hopefully this may help someone :)

Keep UILabel centered using Auto Layout

I'm trying to keep my UILabel centered horizontally. I have tried in IB setting leading and trailing, but that simply stretches out the entire label, and I need it to stay the same size. Everything is set up using Editor and the Pin function in IB and not coding. Suggestions?
If you want to keep all the time buttons together and aligned to the center. Put them all in their own view, arrange how you wish. Then fix the length and width of the view and center it in the container like Aaron suggested. I do believe you will need to make one more constraint. Perhaps pin the view to the top. Hope this helps.
Seems too obvious, but did you try nsconstraint of width = XXX. Label should say same width and the leading trailing should keep it put.
For keeping a UILabel horizontally centre, add the following constraints:-
Width constraint to the label.
Height constraint to the label.
Top/Bottom Constraint for placing the label vertically.
Horizontal centre in container.

Resources