Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式显示和隐藏容器UIView及其子视图_Ios_Swift_Autolayout - Fatal编程技术网

Ios 以编程方式显示和隐藏容器UIView及其子视图

Ios 以编程方式显示和隐藏容器UIView及其子视图,ios,swift,autolayout,Ios,Swift,Autolayout,我有一个标签和一个按钮并排排列,如下所示: @IBAction func show(_ sender: Any) { if let constraint = (containerView.constraints.filter{$0.firstAttribute == .height}.first) { constraint.isActive = false } containerView.layoutIfNeeded() } @IBAction fu

我有一个标签和一个按钮并排排列,如下所示:

@IBAction func show(_ sender: Any) {

    if let constraint = (containerView.constraints.filter{$0.firstAttribute == .height}.first) {
        constraint.isActive = false
    }

    containerView.layoutIfNeeded()
}

@IBAction func hide(_ sender: Any) {
    containerView.heightAnchor.constraint(equalToConstant: 0).isActive = true
    containerView.layoutIfNeeded()

}

这是由以下代码创建的:

private func addViewBack() {
    myLabel.translatesAutoresizingMaskIntoConstraints = false
    myLabel.text = "Show my label"

    myButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
    myButton.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
    myButton.translatesAutoresizingMaskIntoConstraints = false
    myButton.setTitle("Button", for: .normal)
    myButton.clipsToBounds = true

    containerView.backgroundColor = .red
    containerView.addSubview(myLabel)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(myButton)

    containerView.addConstraint(NSLayoutConstraint(item: myButton, attribute: .centerY, relatedBy: .equal, toItem: myLabel, attribute: .centerY, multiplier: 1, constant: 0))
    containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[myLabel]-8-|", options: [], metrics: nil, views: ["myLabel":myLabel]))
    containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[myLabel]", options: [], metrics: nil, views: ["myLabel":myLabel]))
    containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[myButton]-8-|", options: [], metrics: nil, views: ["myButton":myButton]))
    containerView.layoutIfNeeded()

    self.view.addSubview(containerView)

    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[topView]-100-[containerView]", options: [], metrics: nil, views: ["containerView":containerView,"topView":viewTop]))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[containerView]-8-|", options: [], metrics: nil, views: ["containerView":containerView]))
    self.view.layoutIfNeeded()
}
我有“显示”和“隐藏”按钮,按下这些按钮时,它们应该显示和隐藏容器视图,如下所示:

@IBAction func show(_ sender: Any) {

    if let constraint = (containerView.constraints.filter{$0.firstAttribute == .height}.first) {
        constraint.isActive = false
    }

    containerView.layoutIfNeeded()
}

@IBAction func hide(_ sender: Any) {
    containerView.heightAnchor.constraint(equalToConstant: 0).isActive = true
    containerView.layoutIfNeeded()

}
使用“隐藏”操作时,按钮永远不会隐藏,我会得到如下结果:

当我添加以下内容时,它会起作用:

@IBAction func hide(_ sender: Any) {
    containerView.heightAnchor.constraint(equalToConstant: 0).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: 0).isActive = true
    containerView.layoutIfNeeded()

}
当我按下show action(显示动作)时,该按钮不会再次出现,如下所示:


如何使用auto layout以编程方式简单地显示和隐藏容器视图中的元素

不要使容器视图高度约束处于活动/非活动状态,而是将其常量更改为零以隐藏容器视图。如果其
clipstobunds
为true,则也将隐藏内容

不要使容器视图高度约束处于活动/非活动状态,而是将其常量更改为零以隐藏容器视图。如果其
clipstobunds
为true,则也将隐藏内容