简体   繁体   English

UIStackView 自己的内在内容大小

[英]UIStackView's own intrinsicContentSize

I'm using the UIStackView with the following configuration:我正在使用具有以下配置的 UIStackView:

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 .每个元素都有自己的intrinsicContentSize因此UIStackView应该可以提供自己的intrinsicContentSize The documentation states that the spacing is used as a minimum spacing.文档指出spacing用作最小间距。

Example:例子:

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

The intrinsicContentSize.width of the stackView should be 30 .intrinsicContentSize.width的stackView应该是30

Instead I get:相反,我得到:

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

which tells me that the intrinsicContentSize cannot be provided.这告诉我不能提供intrinsicContentSize大小。

Does anyone of you know if I doing something wrong, if the behaviour is intended or if this is a bug?你们中有人知道我是否做错了什么,行为是否是故意的,或者这是一个错误吗?

As of iOS 9.1, UIStackView doesn't implement intrinsicContentSize :从 iOS 9.1 开始, UIStackView没有实现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.如果你真的需要它,你可以创建一个UIStackView的子类并自己实现它。 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).如果UIStackView被允许(通过它的约束)选择它自己的大小,它会根据其排列的子视图的内在内容大小(或您对其排列的子视图的大小设置的其他约束)来选择它自己的大小。

the UIViews you created have an intrinsic height and width of zero.您创建的 UIViews 的固有高度和宽度为零。 Try using autolayout constraints instead for the underlying UIViews.尝试对底层 UIView 使用自动布局约束。

You can also use autolayout in order to size the UIStackView, if you do this use do not set alignment to center, you should use fill instead.您还可以使用自动布局来调整 UIStackView 的大小,如果您这样做,请不要将对齐设置为居中,您应该使用填充来代替。

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 stackview now has a height and width.这将起作用,因为 stackview 现在具有高度和宽度。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM