Open patchthecode opened 7 years ago
I'm no expert on the behavior of UIStackView, and it's possible that this is behaving differently when using Layout than it would if you created it programmatically, but it seems like if you're using distribution=fill then it's going to try to stretch the views to fit the height of the container, and if you don't provide enough context for which view you want it to stretch, it may not stretch the one you want.
Explicitly setting the height of the inner UIStackView to 200 (or any value > 180, in fact, since that's the remaining space to be filled after the 100pts + 20pts padding) solves the problem. Perhaps that's not desirable in the general case, but it seems to work for this example at least.
(SideNote: the sample codes i provide is in XCode 9. If you need another version let me ok. thanks.)
I thought this at first. But then I coded the view programmatically. here is the code (xcode9) -> Layout.zip
When we debug the frame hierarchy in XCode we see that the innerStackView
stretches to fill the available space.
I also created a UIStackView on interfaceBuilder, and it also behaves the same way.
According to the description on fill
A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. When the arranged views do not fit within the stack view, it shrinks the views according to their compression resistance priority. If the arranged views do not fill the stack view, it stretches the views according to their hugging priority. If there is any ambiguity, the stack view resizes the arranged views based on their index in the arrangedSubviews array.
Since the redSquare has a fixed content size, the UIStackView should honor this. The 2 labels do not have and fixed size, so the stackView should stretch them like it does programatically, or on interface builder; If done this way, it will then respect the priority of the fixed square first, over the priority of the UILabels because a view constant size has more priority than a view without one.
Disclaimer: I am no NSLayoutConstraint expert either, so maybe someone else can comment on this. I just thought that the behavior was different. Maybe I can further help if you let me know in which parts the layoutConstraints are setup? I am new to this code, and couldn't trace it.
Layout doesn't use AutoLayout - it's all frame based. Every Layout-managed view has a fixed height, even if you haven't set one explicitly. In the case of a UIStackView, the height will be set to match the content by default. But that probably prevents it from working exactly the same way as a non Layout-based view.
I'll dig into this a bit more and see what would be involved in making it behave more like iOS-standard. It may be possible to special-case the behavior for views inside a UIStackView.
I didnt know that Autolayout was not used. Thanks for looking into this.
Version: 0.4.17 Xcode: 9.6 Beta SampleCode: LayoutsSample
Above is the image of the result I am getting.
Expected: The red square should be height 100. Actual: It looks like the red square's height value is bigger than it should be. Below is the pasted code.
Thank much.