Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStackView's own intrinsicContentSize

I'm using the UIStackView with the following configuration:

let contentView = UIStackView()
contentView.distribution = .EqualSpacing
contentView.alignment = .Center
contentView.spacing = horizontalSpacing

Each of the elements has it's own intrinsicContentSize so it should be possible for the UIStackView to provide its own intrinsicContentSize. The documentation states that the spacing is used as a minimum spacing.

Example:

view1: width=10
view2: width=15
spacing = 5
[view1(10)]-5-[view2(15)]

The intrinsicContentSize.width of the stackView should be 30.

Instead I get:

▿ CGSize
 - width : -1.0
 - height : -1.0 { ... }

which tells me that the intrinsicContentSize cannot be provided.

Does anyone of you know if I doing something wrong, if the behaviour is intended or if this is a bug?

like image 452
simonseyer Avatar asked Oct 26 '15 14:10

simonseyer


People also ask

What is a UIStackView?

A streamlined interface for laying out a collection of views in either a column or a row.

What is Intrinsiccontentsize?

Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label's intrinsic content size is based on how much text it is displaying. In your case, the image view's intrinsic content size is the size of the image that you selected.

How do I remove all Subviews from UIStackView?

Managing Subviews To remove an arranged subview that you no longer want around, you need to call removeFromSuperview() on it. The stack view will automatically remove it from the arranged subview list.

What is Stackview alignment?

A layout where the stack view aligns the center of its arranged views with its center along its axis. case leading. A layout for vertical stacks where the stack view aligns the leading edge of its arranged views along its leading edge.


2 Answers

As of iOS 9.1, UIStackView doesn't implement intrinsicContentSize:

import UIKit
import ObjectiveC

let stackViewMethod = class_getInstanceMethod(UIStackView.self, "intrinsicContentSize")
let viewMethod = class_getInstanceMethod(UIView.self, "intrinsicContentSize")
print(stackViewMethod == viewMethod)

Output:

true

You could make a subclass of UIStackView and implement it yourself if you really need it. You shouldn't need to, though. If UIStackView is allowed (by the constraints on it) to choose its own size, it does so based on its arranged subviews' intrinsic content size (or by other constraints you've set on the sizes of its arranged subviews).

like image 60
rob mayoff Avatar answered Sep 17 '22 16:09

rob mayoff


Get this instead:

stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

If you wish to fit it in a view you can pass the actual view's frame and desired priorities for the horizontal and vertical fittings. For example, this will preserve the width of the view and size the height:

stackView.systemLayoutSizeFitting(view.frame.size, withHorizontalFittingPriority: .required, verticalFittingPriority: .defaultLow)
like image 43
Mojtaba Hosseini Avatar answered Sep 21 '22 16:09

Mojtaba Hosseini