Ios 如何显示隐藏的UIButton?

Ios 如何显示隐藏的UIButton?,ios,swift,autolayout,Ios,Swift,Autolayout,视图是使用代码放置的。 如果我按deleteButton,我希望隐藏的buttonimgButton出现 但是,imgView的宽度不是刷新 MainViewController.swift 这是错误消息: 2018-05-23 14:00:47.697959+0900 Test[67488:4887863] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the

视图是使用代码放置的。 如果我按deleteButton,我希望隐藏的buttonimgButton出现

但是,imgView的宽度不是刷新

MainViewController.swift

这是错误消息:

2018-05-23 14:00:47.697959+0900 Test[67488:4887863] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "" ) Will attempt to recover by breaking constraint Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
您正在向按钮添加新约束,这些约束与旧约束冲突。一个按钮如何可以宽0点,同时宽50点?。要使其工作,需要在激活新约束之前关闭旧约束。我建议创建一个属性,该属性将始终保持当前imgButton约束,然后当您想要更改它时,只需使用该属性,或者关闭它并创建一个新的imgButton约束,或者只设置一个常量,这在您的情况下会更好、更容易:

import UIKit

class MainViewController: UIViewController {

    // property referencing current imgButton width constraint
    fileprivate var imgButtonWidthConstraint: NSLayoutConstraint!

    var trashIsSelected: Bool!

    let imgButton: UIButton = {
        let imgView = UIButton()
        imgView.setImage(UIImage(named: "schedule_delete_icon"), for: UIControlState.normal)
        //        imgView.imageView?.image = UIImage(named: "schedule_delete_icon")
        imgView.translatesAutoresizingMaskIntoConstraints = false
        return imgView
    }()

    let deleteButton: UIButton = {
        let imgBtn = UIButton()
        imgBtn.setImage(UIImage(named: "icon_delete"), for: UIControlState.normal)
        //        imgBtn.imageView?.image = UIImage(named: "icon_delete")
        imgBtn.translatesAutoresizingMaskIntoConstraints = false
        return imgBtn
    }()

    let label: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "하ㅣㅇ하이히아히아하하하하ㅏㅎ하ㅏㅎ하ㅏ하하하하ㅏㅏ하하하하하ㅏ"
        return label
    }()


    var imgViewWidth: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupLayout()
    }


    func setupLayout(){
        let testView = UIScrollView()

        self.view.addSubview(testView)
        testView.backgroundColor = .lightGray
        testView.translatesAutoresizingMaskIntoConstraints = false
        testView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        testView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        testView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        testView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.3).isActive = true

        testView.addSubview(imgButton)
        testView.addSubview(label)
        imgButton.leadingAnchor.constraint(equalTo: testView.leadingAnchor, constant: 10).isActive = true
        imgButton.centerYAnchor.constraint(equalTo: testView.centerYAnchor).isActive = true
        imgButton.heightAnchor.constraint(equalTo: testView.heightAnchor, multiplier: 0.2).isActive = true

        // keep the reference to constraint that defines width
        // (we will use the constraint setting the width to constant, since then you can
        // simply switch the constant between 0 and 50):
        imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalToConstant: 0)
        imgButtonWidthConstraint.isActive = true

        imgButton.isHidden = true
        trashIsSelected = false

        label.centerYAnchor.constraint(equalTo: testView.centerYAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: imgButton.trailingAnchor, constant: 10).isActive = true
        label.heightAnchor.constraint(equalTo: testView.heightAnchor, multiplier: 0.2).isActive = true
        label.widthAnchor.constraint(equalTo: testView.widthAnchor, multiplier: 0.5).isActive = true




        self.view.addSubview(deleteButton)
        deleteButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        deleteButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        deleteButton.addTarget(self, action: #selector(self.addBtnAction(_:)), for: UIControlEvents.touchUpInside)
    }

    @objc func addBtnAction(_ sender: UIButton){
        print("hi")
        if trashIsSelected == false{
            trashIsSelected = true
            imgButton.isHidden = false

            // just change the constant to what you want
            imgButtonWidthConstraint.constant = 50

            imgButton.updateConstraints()

        } else {
            trashIsSelected = false

            imgButtonWidthConstraint.constant = 0

            imgButton.isHidden = true
            imgButton.updateConstraints()
        }
    }
}
编辑:

为了完整地回答这个问题,如果出于某种原因使用约束,而仅仅更改常量是不够的,则必须激活和停用约束。例如,如果使用乘法器确定imgButton的宽度,则必须使用此方法乘法器是NSLayoutConstraint的不可变属性。因此,创建一个约束:

// simply switch the constant between 0 and 50):
imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalTo: someOtherView.widthAnchor, multiplier: 0)
imgButtonWidthConstraint.isActive = true
除此之外,您还必须执行以下操作:

@objc func addBtnAction(_ sender: UIButton){
    print("hi")
    if trashIsSelected == false{
        trashIsSelected = true
        imgButton.isHidden = false

        // first deactivate current constraint
        imgButtonWidthConstraint.isActive = false
        // then create a new one and store it to imgButtonWidthConstraint property (the old one is deactivated, so you don't need a reference to it anymore)
        imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalTo: someOtherView.widthAnchor, multiplier: 0.75)
        // and activate the new one
        imgButtonWidthConstraint.isActive = true

        imgButton.updateConstraints()

    } else {
        trashIsSelected = false

        // same process again
        imgButtonWidthConstraint.isActive = false
        imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalTo: someOtherView.widthAnchor, multiplier: 0)
        imgButtonWidthConstraint.isActive = true

        imgButton.isHidden = true
        imgButton.updateConstraints()
    }
}

非常感谢你。多亏了这一点,我解决了我们长期以来一直在思考的问题。
@objc func addBtnAction(_ sender: UIButton){
    print("hi")
    if trashIsSelected == false{
        trashIsSelected = true
        imgButton.isHidden = false

        // first deactivate current constraint
        imgButtonWidthConstraint.isActive = false
        // then create a new one and store it to imgButtonWidthConstraint property (the old one is deactivated, so you don't need a reference to it anymore)
        imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalTo: someOtherView.widthAnchor, multiplier: 0.75)
        // and activate the new one
        imgButtonWidthConstraint.isActive = true

        imgButton.updateConstraints()

    } else {
        trashIsSelected = false

        // same process again
        imgButtonWidthConstraint.isActive = false
        imgButtonWidthConstraint = imgButton.widthAnchor.constraint(equalTo: someOtherView.widthAnchor, multiplier: 0)
        imgButtonWidthConstraint.isActive = true

        imgButton.isHidden = true
        imgButton.updateConstraints()
    }
}