简体   繁体   English

为stackView中的项目设置最大高度的约束

[英]Constraint to set max height for items inside stackView

I have a stackView with UiViews boxes inside as you can see in the picture.如图所示,我有一个带有 UiViews 框的 stackView。 I would like to set a max height of 50 or less for the boxes inside as they look too big right now but i also would like to keep the 1:1 ratio so that they would be square.我想为里面的盒子设置一个 50 或更小的最大高度,因为它们现在看起来太大了,但我也想保持 1:1 的比例,这样它们就会是方形的。 I've tried to set a height constraint to the stackView but when i do that, the aspect ratio of 1:1 is being ignored.我试图为 stackView 设置一个高度约束,但是当我这样做时,1:1 的纵横比被忽略了。

Here's what it looks like right now:这是它现在的样子:

在此处输入图像描述

And here's the code:这是代码:

let stackview = UIStackView(arrangedSubviews: letterBoxes)
        stackview.axis = .horizontal
        stackview.spacing = 5
        stackview.distribution = .fillEqually
        stackview.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(stackview)        
        NSLayoutConstraint.activate([
            stackview.topAnchor.constraint(equalTo: secondLine.bottomAnchor, constant: c/2 +
                20),
            stackview.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            stackview.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10)
            ])

        for box in letterBoxes{
            box.aspectRation(1.0/1.0).isActive = true
        }

Edit: i want the box to auto-resize and not overflow like the following (each time there's a different amount of boxes. so for example when there are only 3 boxes i want that the max size will be 50 but when there are 6 or higher number I want it to auto-resize so that it will fit the screen and not overflow. once I take the leading constraint off it overflows):编辑:我希望盒子自动调整大小并且不会像下面那样溢出(每次都有不同数量的盒子。所以例如,当只有 3 个盒子时,我希望最大尺寸为 50,但当有 6 个或更大的数字我希望它自动调整大小,以便它适合屏幕并且不会溢出。一旦我取消领先的约束,它就会溢出): 在此处输入图像描述

The following will do the job.以下将完成这项工作。

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let letterBoxes = [UIView(), UIView(), UIView()]
        for box in letterBoxes {
            box.translatesAutoresizingMaskIntoConstraints = false
            box.backgroundColor = .systemBlue
            view.addSubview(box)

            let heightConstraint = box.heightAnchor
                .constraint(equalToConstant: 50)
            heightConstraint.priority = .defaultHigh

            NSLayoutConstraint.activate([
                box.widthAnchor.constraint(equalTo: box.heightAnchor),
                heightConstraint
            ])
        }

        let stackview = UIStackView(arrangedSubviews: letterBoxes)
        stackview.axis = .horizontal
        stackview.spacing = 5
        stackview.distribution = .fillEqually
        stackview.alignment = .center
        stackview.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackview)

        NSLayoutConstraint.activate([
            stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stackview.leadingAnchor.constraint(
                greaterThanOrEqualTo: view.leadingAnchor,
                constant: 10
            ),
            stackview.trailingAnchor.constraint(
                lessThanOrEqualTo: view.trailingAnchor,
                constant: -10
            )
        ])
    }
}

The point is to make the priorities of the heighConstraint s for the boxes to be less than required .关键是要使盒子的heighConstraint的优先级小于required So when there are not a lot of boxes, they will be of size 50, but when there are many, the height constraints will be ignored.所以当盒子不多的时候,它们的大小是 50,但是当盒子很多时,高度限制将被忽略。

Also note that the stackview 's leadingAnchor and the trailingAnchor constraint keeps the boxes in the screen bounds.另请注意, stackviewleadingAnchortrailingAnchor约束将框保持在屏幕边界内。

The following screenshots show the result:以下屏幕截图显示了结果:

在此处输入图像描述 在此处输入图像描述

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

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