简体   繁体   English

如何在 IOS 中的 UIStackView 中设置权重

[英]How to set weight in UIStackView in IOS

UIStackView is similar to Android LinearLayout but I could not figure out how to set weight for the subviews. UIStackView类似于 Android LinearLayout但我不知道如何为子视图设置权​​重。

Suppose I have a vertical UIStackView and 3 UIImageView s in it.假设我有一个垂直的UIStackView和 3 个UIImageView I want to set weights 3, 6, 1 consecutively for the UIImageView s.我想为UIImageView连续设置权重UIImageView How do I do that?我怎么做?

布局图

UIStackView doesn't have the same concept of weights. UIStackView没有相同的权重概念。 It can use a subview's intrinsicContentSize as a weight, but setting a specific intrinsicContentSize typically requires making a subclass and it's used in other situations too (unlike the android:layout_weight attribute you're familiar with, which is only used by LinearLayout ).它可以使用子视图的intrinsicContentSize作为权重,但是设置特定的intrinsicContentSize通常需要创建一个子类,并且它也用于其他情况(与您熟悉的android:layout_weight属性不同,它仅由LinearLayout )。

But since UIStackView works by applying constraints to its arranged subviews, you can get the effect of weights by setting additional constraints between the heights of the subviews.但是由于UIStackView通过对其排列的子视图应用约束来工作,因此您可以通过在子视图的高度之间设置附加约束来获得权重的效果。 ( UIStackView is designed to let you add your own constraints to tweak the layout this way.) UIStackView旨在让您添加自己的约束以这种方式调整布局。)

In your case, you want to constrain the height of the top view to be 3 times the height of the bottom view, and you want to constrain the height of the middle view to be 6 times the height of the bottom view.在您的情况下,您希望将顶视图的高度限制为底视图高度的 3 倍,并将中间视图的高度限制为底视图高度的 6 倍。

You can set up a proportional-height constraint in a storyboard by creating an equal-height constraint, then editing the constraint's multiplier.您可以通过创建等高约束,然后编辑约束的乘数,在故事板中设置比例高度约束。

In code, you could do it like this (on iOS 9.0 or later):在代码中,您可以这样做(在 iOS 9.0 或更高版本上):

NSLayoutConstraint.activateConstraints([
    top.heightAnchor.constraintEqualToAnchor(bottom.heightAnchor, multiplier: 3),
    middle.heightAnchor.constraintEqualToAnchor(bottom.heightAnchor, multiplier: 6),
])

Intrinsic content size, etc, is totally uninvolved.内在内容大小等完全不涉及。

To set fractional heights, just set fractional heights:要设置分数高度,只需设置分数高度:

  1. Fix the height of the stack view (say, the whole screen)修复堆栈视图的高度(比如整个屏幕)

  2. Put in the three views A, B, C放入三个视图A,B,C

  3. For A, make an height constraint 0.3 of the height of the stack view对于 A,将堆栈视图的高度限制为 0.3

  4. For B, make an height constraint 0.6 of the height to the stack view对于 B,将高度约束为堆栈视图的 0.6

Set the stack view to distribution:Fill.将堆栈视图设置为分布:填充。

If you run this on a powerful iPhone, it will figure out that C is "0.1".如果您在功能强大的 iPhone 上运行它,它会发现 C 是“0.1”。

You're done.你完成了。

First off, do you really need a stack view for this?首先,你真的需要一个堆栈视图吗? It would be much easier to arrange this simply using proportional height constraints directly.直接使用比例高度约束来安排它会容易得多。

However, you can do this with a stack view if you really want to use a stack view.但是,如果您确实想使用堆栈视图,则可以使用堆栈视图执行此操作。 The secret is that the "weight" in question is simply the arranged view's intrinsicContentSize().height .秘密在于,所讨论的“重量”只是排列视图的intrinsicContentSize().height Knowing this, I was easily able to set up a stack view consisting of three image views in the proportions you request:知道了这一点,我可以轻松地设置一个堆栈视图,该视图由您要求的比例的三个图像视图组成:

在此处输入图片说明

Those, for purposes of the demonstration, are the same image repeated three times: one at 3x height, one at 6x height, and one at 1x height.出于演示的目的,这些图像是重复了 3 次的相同图像:一张高度为 3 倍,一张高度为 6 倍,另一张高度为 1 倍。

How did I do it?我是怎么做的? I gave the three image views tag values of 300, 600, and 100 respectively in the storyboard.我在故事板中分别给了三个图像视图tag值 300、600 和 100。 (Of course I could have used an IBInspectable custom property for this, and in real life, I would do so.) Then I made them all instances of my UIImageView subclass, MyImageView, whose code looks like this: (当然,我可以为此使用 IBInspectable 自定义属性,在现实生活中,我会这样做。)然后我让它们成为我的 UIImageView 子类 MyImageView 的所有实例,其代码如下所示:

class MyImageView: UIImageView {
    override func intrinsicContentSize() -> CGSize {
        print(self.tag)
        return CGSizeMake(CGFloat(self.tag), CGFloat(self.tag))
    }
}

The stack view's Distribution is configured as Fill Proportionally.堆栈视图的分布配置为按比例填充。 The image views are configured with their Content Mode as Scale To Fill.图像视图的内容模式配置为按比例填充。 Result: The stack view, in laying out its arranged views, consults the intrinsicContentSize method, and thus does the right thing.结果:堆栈视图在布置其排列的视图时,会参考intrinsicContentSize方法,从而做正确的事情。

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

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