UIStackView native intrinsicContentSize

I am using UIStackView with the following configuration:

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

Each element has its own intrinsicContentSize , so UIStackView should be possible for UIStackView to provide its own intrinsicContentSize . The documentation states that spacing used as the minimum interval.

Example:

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

intrinsicContentSize.width stackView should be 30 .

Instead, I get:

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

which tells me that intrinsicContentSize cannot be provided.

Do any of you know that I am doing something wrong if this is behavior or if it is a mistake?

+8
ios objective-c uikit swift uistackview
source share
2 answers

As in iOS 9.1, UIStackView does not 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 can subclass UIStackView and implement it yourself if you really need it. But you do not need it. If the UIStackView allowed (by restrictions on it) to choose its own size, it does this based on its own size of the contents of the built-in sub-account (or other restrictions that you set on the size of its organized sub-views).

+10
source share

The UIView you created has an internal height and width of zero. Try using autodetection restrictions instead of the base UIView.

You can also use autostart so that the size of the UIStackView, if you use it, do not set the alignment to the center, you should use fill instead.

Example:

 @property (nonatomic, strong) UIStackView *firstStackView; @property (nonatomic, strong) UIView *redView; @property (nonatomic, strong) UIView *blueView; @property (nonatomic, strong) UIView *yellowView; @property (nonatomic, strong) UIView *greenView; @property (nonatomic, strong) NSArray *subViews; self.redView = [[UIView alloc] init]; self.redView.backgroundColor = [UIColor redColor]; self.blueView = [[UIView alloc] init]; self.blueView.backgroundColor = [UIColor blueColor]; self.yellowView = [[UIView alloc] init]; self.yellowView.backgroundColor = [UIColor yellowColor]; self.greenView = [[UIView alloc] init]; self.greenView.backgroundColor = [UIColor blackColor]; self.subViews = @[self.greenView,self.yellowView,self.redView,self.blueView]; self.firstStackView = [[UIStackView alloc] initWithArrangedSubviews:self.subViews]; self.firstStackView.translatesAutoresizingMaskIntoConstraints = NO; self.firstStackView.distribution = UIStackViewDistributionFillEqually; self.firstStackView.axis = UILayoutConstraintAxisHorizontal; self.firstStackView.alignment = UIStackViewAlignmentFill; [self.firstStackView.heightAnchor constraintEqualToConstant:40].active = YES; [self.firstStackView.widthAnchor constraintEqualToConstant:500].active = YES; 

This will work because the stack now has height and width.

0
source share

All Articles