Попробуйте с этим кодом ниже.
import UIKit
class MyViewController: UIViewController {
let searchesShadowView = UIView()
let plusButton = UIButton()
let minusButton = UIButton()
var heightLayoutConstraint:NSLayoutConstraint?
let value: CGFloat = 10.0
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = .yellow
loadUIView()
loadPlusButton(plusButton)
loadMinusButton(minusButton)
}
func loadUIView() {
self.view.addSubview(searchesShadowView)
searchesShadowView.backgroundColor = .red
searchesShadowView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: searchesShadowView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 200).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
heightLayoutConstraint = NSLayoutConstraint(item: searchesShadowView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
heightLayoutConstraint?.isActive = true
}
func loadPlusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 1
button.backgroundColor = UIColor.green
button.setTitle("+", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
func loadMinusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 0
button.backgroundColor = UIColor.green
button.setTitle("-", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
@objc func buttonAction(sender: UIButton!) {
UIView.animate(withDuration: 0, delay: 0.2, options: UIView.AnimationOptions.curveEaseOut, animations: {
if sender.tag == 1 {
self.heightLayoutConstraint?.constant += self.value
}else{
self.heightLayoutConstraint?.constant -= ((self.heightLayoutConstraint?.constant ?? 0)-self.value) >= 0 ? self.value : 0
}
self.view.layoutIfNeeded()
}) { (value) in }
}
}