How do I use a UIStackView in a non filling vertical capacity?

How do I use a UIStackView in a non filling vertical capacity?



I want to stack varying height UIViews (each built in xibs) beneath the other (with a width that simply expands to meet the container) and I'm finding this hard probably because I don't understand it.



My situation is relatively simple. I have a dashboard view controller that has a vertically stacked UIStackView which should take 1 or more components that are dynamically added from code. The components are xib views that are each linked back to UIViewControllers which themselves are 'free form' views loaded dynamically. What I want is for them to be stacked as per their heights (not filled, or stretched or ratio filled but fixed to their intrisic heights) and with the appropriate space left at the bottom if space is available. With free form, the height itself seems to be ignored however...



As a result of them being xibs, it does not seem possible to set a 'maximum' (instrinsic) height within the view as only the controls within seem to have this as a property.



My views are based on uiviewcontrollers that I have added as follows:


override func viewDidLoad()
super.viewDidLoad()

let component1 = Component1Presenter(nibName: "Component1View", bundle: nil);
self.addChildViewController(component1)

let component2 = Component2Presenter(nibName: "Component2View", bundle: nil);
self.addChildViewController(component2)

let component3 = Component3Presenter(nibName: "Component3View", bundle: nil);
self.addChildViewController(component3)

let component4 = Component4Presenter(nibName: "Component4View", bundle: nil);
self.addChildViewController(component4)

_dashboardComponentStack.addArrangedSubview(component1.view)
_dashboardComponentStack.addArrangedSubview(component2.view)
_dashboardComponentStack.addArrangedSubview(component3.view)
_dashboardComponentStack.addArrangedSubview(component4.view)



I've tried every variant of the UIStackView I think!, and in all cases they stretch or fill or exclude some of my views. My assumption is that the stack would take the intrinsic height of my views and stack them accordingly with the very last filling the remaining space. I'm clearly missing something obvious or very subtle or I'm just so very new to this to have missed the point. Just a reminder again, is this the right control for me to avoid stretching or filling a stacked set of views?





I'm sorry @Honey I meant no disrespect. I'm a bit tired and I honestly thought my question was valid. I'm a bit new to Swift having taken a sabbatical for about 3 years (previously objective c) and I think I've missed the wood through the trees. With genuine desire I truly want to know how to fix this and will continue with my search. I'm sorry.
– The Senator
Aug 20 at 16:57





just as a side note, not filled, or stretched or ratio filled but fixed to their intrisic heights What do you expect the intrinsiccontentSize to be based off of? Because plain UIViews don't have intrinsicContentSize. If they you have a label, textField, textView, button then you do have intrinsicContentSize otherwise you get nothing. Not sure, but this article might help to understand where intrinsicContentSize value is used to calculate constraints when there's also a stackview involved.
– Honey
Aug 21 at 2:16



intrinsicContentSize





@Honey thanks, I'll have a read of that article. Thanks for sticking with me. It is appreciated.
– The Senator
Aug 21 at 8:32




1 Answer
1



It is hard to give you a definitive answer, but here's an attempt at an explanation that may lead you in the right direction.



The stackview layout is based on constraints. You are correct that if the subviews all have intrinsic content sizes, then the stackview should be able to figure it out, or give you some error information, in the console.
Don't forget that you may need to manipulate the content compression resistance, or the content hugging priority, to resolve any ambiguity there may be about which view's intrinsic content size takes priority over the other's.



From the way you phrase it, it seems you might have misunderstood how stackviews distribute the subviews. It is not the last subview that fills any remaining space. How the space is filled is entirely based on the auto-layout properties if the subviews, such as intrinsic content size, content compression resistance, content hugging priority and any other constraints you may have wired between the subviews.



A view may be 'excluded' if there is not sufficient space to satisfy all the intrinsic content sizes. If you have not made it explicit how to handle that through constraints and/or setting content compression resistance, then the auto-layout engine will attempt to 'guess' which view is not important and therefore can be compressed, sometimes to the effect of entirely removing it.





Can I place perhaps a 'dummy' view that could occupy the remaining space? I can understand that I can get various views to share equally the space, or to ratio arrange the space. But I'd expect a stack view to not expand or fill by default, but more as a request. Is this the wrong control for me? Should I use a table control instead? (seems overkill however)
– The Senator
Aug 20 at 17:14





A stackview will never leave 'empty' space. You do need to place an empty dummy view, or make the topAnchor constraint between the stackview and the containing view able to adapt to a smaller stackview, i.e use a lower priority for that constraint or use a greaterThenOrEqual constraint. That way you would get 'empty' space between the top of the stackview and the view containing the stackview. I think you have the right view, you just need more experience with it. These days most tableviews should use dynamic cell heights anyway and that requires auto-layout too
– Marmoy
Aug 20 at 17:20






Thanks, I think you are right it's certainly a lack of experience coupled with a late night of frustration. I have had a go at the dummy view approach, but I think as @Honey says in my question above I need to read more about intrinsic content size before I can get it. It's a bit like understanding the constraints system one day it just clicks and it's obvious!
– The Senator
Aug 21 at 8:32






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj