简体   繁体   English

AutoLayout 未以编程方式更新视图 - Swift

[英]AutoLayout not updating views programmatically - Swift

I have the following code where I'm creating and laying out everything in code.我有以下代码,我在其中创建和布置代码中的所有内容。 What I'm trying to do is modify the myTopContainer when a button is tapped but I can not make the width changed, only the height gets modified.我想要做的是在点击按钮时修改myTopContainer但我不能改变宽度,只有高度被修改。

Any idea why?知道为什么吗?

func setupAutoLayout(){
    NSLayoutConstraint.activate([
        // My Container
        myTopContainer.bottomAnchor.constraint(equalTo: myButton.topAnchor, constant: -5),
        myTopContainer.heightAnchor.constraint(equalToConstant: 50),
        myTopContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
        myTopContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15),

        // My Button
        myButton.widthAnchor.constraint(equalToConstant: 80),
        myButton.heightAnchor.constraint(equalToConstant: 40),
        myButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -15),
        myButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -400)
    ])
}


@IBAction func relayoutViews(_ sender: Any) {

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {

        self.myTopContainer.heightAnchor.constraint(equalToConstant: 50).isActive = false
        self.myTopContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 15).isActive = false

        // this is working, it changes the height
        self.myTopContainer.heightAnchor.constraint(equalToConstant: 10).isActive = true
        // this is not doing anything
        self.myTopContainer.leadingAnchor.constraint(equalTo: self.myButton.trailingAnchor).isActive = true

        self.view.layoutIfNeeded()

    })
}

在此处输入图像描述

A line like this像这样的一行

self.myTopContainer.heightAnchor.constraint(equalToConstant: 50).isActive = false

Creates a new constraint every time.每次创建一个新的约束。 It doesn't modify the previously created constraint.它不会修改先前创建的约束。

When you setupAutolayout you should save a reference to the constraints you want to later modify before actually calling NSLayoutConstraint.activate() on them.当您 setupAutolayout 时,您应该在实际调用NSLayoutConstraint.activate()之前保存对稍后要修改的约束的引用。 Later you can set isActive to false on them.稍后您可以在它们上将 isActive 设置为 false。

For example:例如:

var someConstraint: NSLayoutConstraint?
func setupAutoLayout(){
    let aConstraint = myTopContainer.bottomAnchor.constraint(equalTo: myButton.topAnchor, constant: -5)
    someConstraint = aConstraint
    NSLayoutConstraint.activate([
        // My Container
        aConstraint
    ])
}

func editConstraint() {
    self.someConstraint?.isActive = false
}

Anyway be careful on logs when you edit constraints, it's probable that you could break something while editing.无论如何,当你编辑约束时要小心日志,你可能会在编辑时破坏一些东西。 If you see those logs of broken constraint than it means that something is still wrong (even if it visually looks good, it could crash or not work properly in some other situation)如果您看到那些破坏约束的日志,则意味着仍然有问题(即使看起来不错,它也可能在其他情况下崩溃或无法正常工作)

You're missing:你错过了:

view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false

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

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