简体   繁体   English

以编程方式更改UIStackView中1个子视图的大小

[英]Programmatically changing size of 1 subview in UIStackView

I am currently making a calculator and want to change the size of my 0 button to the size of 2 subviews - which is half the size of the entire view. 我目前正在制作计算器,并且想将我的0按钮的大小更改为2个子视图的大小-这是整个视图大小的一半。 I want it to look exactly like apples calculator app, where the 0 is bigger than all the other buttons. 我希望它看起来完全像苹果计算器应用程序,其中0大于所有其他按钮。
The way i layout my view is by having a vertical UIStackView and adding horizontal UIStackView 's to it, just like the picture below. 我布局视图的方式是通过具有垂直的UIStackView并向其添加水平的UIStackView ,如下图所示。 在此处输入图片说明

Therefore, i want the last horizontal stack to have 3 arranged subviews but make the 0 button fill the exceeding space, so the , and = buttons are the same size as all other buttons. 因此,我希望最后一个水平堆栈具有3个排列的子视图,但使0按钮填充超出的空间,因此,=按钮的大小与所有其他按钮的大小相同。

Thank you. 谢谢。

You can use stackview.distribution = .fillEquallly for first 4 horizontal stackviews and use stackview.distribution = .fillPropotionally for the last horizontal stackview. 您可以将stackview.distribution = .fillEquallly用于前四个水平stackview,并将stackview.distribution = .fillPropotionally用于最后一个水平stackview。

Then set with constraint for the 0 button to 50% of last horizontal stackview's width. 然后将0按钮的约束条件设置为最后一个水平stackview宽度的50%。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Programmatically , you could set multiplier with constraint(equalTo:multiplier:) , official docs: https://developer.apple.com/documentation/uikit/nslayoutdimension/1500951-constraint 以编程方式 ,您可以设置带有constraint(equalTo:multiplier:) ,官方文档: https : //developer.apple.com/documentation/uikit/nslayoutdimension/1500951-constraint

So we could constraint the last two button with same width and make the first one two times longer than one of the other two. 因此我们可以用相同的宽度约束最后两个按钮,并使第一个按钮的长度是其他两个按钮的两倍。

    override func viewDidLoad() {
        super.viewDidLoad()


        view.backgroundColor = .white

        let btn1 = UIButton()
        let btn2 = UIButton()
        let btn3 = UIButton()

        btn1.backgroundColor = .red
        btn2.backgroundColor = .yellow
        btn3.backgroundColor = .blue

        let hStack = UIStackView(arrangedSubviews: [btn1, btn2, btn3])
        hStack.axis = .horizontal
        hStack.spacing = 1

        view.addSubview(hStack)
        hStack.translatesAutoresizingMaskIntoConstraints = false
        hStack.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        hStack.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        hStack.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        // Here would be what you need:
        btn2.widthAnchor.constraint(equalTo: btn3.widthAnchor).isActive = true
        btn1.widthAnchor.constraint(equalTo: btn2.widthAnchor, multiplier: 2).isActive = true
    }

在此处输入图片说明

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

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