how to change to before and after constraints when

2019-07-28 18:27发布

问题:

This code has a button that when pressed the blue square above the button becomes smaller. But when pressing the button again the square does not revert back to its original size. This is because the buttons func only has one command. How can I command the button to go back and forth between its original size and smaller size by only pressing one button. This is swift code.

//
//  ViewController.swift
//  ssfsfd
//
//  Created by John Zalubski on 8/28/16.
//  Copyright © 2016 John Zalubski. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let colorview = UIView()
    var initialc = [NSLayoutConstraint]()
    override func viewDidLoad() {
        super.viewDidLoad()
        colorview.translatesAutoresizingMaskIntoConstraints = false
        colorview.backgroundColor = UIColor.blueColor()
        self.view.addSubview((colorview))

        let leadingc = colorview.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor)
        let trailingC = colorview.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor)
        let topc = colorview.topAnchor.constraintEqualToAnchor(self.view.topAnchor)
        let bottomc = colorview.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: -50)

        initialc.appendContentsOf([leadingc,trailingC,topc,bottomc])
        NSLayoutConstraint.activateConstraints(initialc)


    }
    @IBAction func changethebleep(sender: AnyObject) {

        NSLayoutConstraint.deactivateConstraints(initialc)

        let widthc = colorview.widthAnchor.constraintEqualToConstant(100)
        let heightc = colorview.heightAnchor.constraintEqualToConstant(100)
        let centerxc = colorview.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor)
        let centeryc = colorview.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor)

        NSLayoutConstraint.activateConstraints([widthc,heightc,centerxc,centeryc])

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

回答1:

Here is one way to do it. Put all of the constraints in an array called allc. Only half of the constraints will be active at any time.

In your button handler, change the active constraints to inactive, and then make the previously inactive ones be active:

class ViewController: UIViewController {

    let colorview = UIView()
    var allc = [NSLayoutConstraint]()
    override func viewDidLoad() {
        super.viewDidLoad()
        colorview.translatesAutoresizingMaskIntoConstraints = false
        colorview.backgroundColor = UIColor.blueColor()
        self.view.addSubview((colorview))

        let leadingc = colorview.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor)
        let trailingC = colorview.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor)
        let topc = colorview.topAnchor.constraintEqualToAnchor(self.view.topAnchor)
        let bottomc = colorview.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: -50)

        NSLayoutConstraint.activateConstraints([leadingc, trailingC, topc, bottomc])

        let widthc = colorview.widthAnchor.constraintEqualToConstant(100)
        let heightc = colorview.heightAnchor.constraintEqualToConstant(100)
        let centerxc = colorview.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor)
        let centeryc = colorview.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor)

        allc = [leadingc, trailingC, topc, bottomc, widthc, heightc, centerxc, centeryc]
    }

    @IBAction func changethebleep(sender: AnyObject) {

        var newactive = [NSLayoutConstraint]()

        for constraint in allc {
            if constraint.active {
                constraint.active = false
            } else {
                newactive.append(constraint)
            }
        }

        NSLayoutConstraint.activateConstraints(newactive)
    }
}

Note: It is necessary to first deactivate constraints before activating new ones. If you deactivate first, you will avoid the situation where your view is over constrained and Auto Layout gives warning messages. This is why the code puts the new constraints to activate into an array so that it can activate them after the previously active ones have been deactivated.