Hi, I'm trouble in making good motion between UILabel and UIButton when UIButton changes its height.
If you click UIButton to result in longer its height, UILabel(red) will be short.
Okay That's good.
But my question is that when UILabel(red) is going to be short why it's bottom anchor is moving shortly up and down ?
I just changed button height constraint when button is clicked. UILabel (red) bottom constraint is still self.view.bottom.anchor.
I questioned this problem to other developer, I get other solution using UIView instead of UILabel.
But they didn't know why UILabel was going to be somethings like error. They thought that was maybe because of UILabel's intrinsicContentSize .
But I still clearly don't know why was happened.
I would appreciate you if you give me clear answer. thanks!
here is my entire code.
you can run exactly just copy and paste.
If you click UIButton to result in longer its height, UILabel(red) will be short.
Okay That's good.
But my question is that when UILabel(red) is going to be short why it's bottom anchor is moving shortly up and down ?
I just changed button height constraint when button is clicked. UILabel (red) bottom constraint is still self.view.bottom.anchor.
I questioned this problem to other developer, I get other solution using UIView instead of UILabel.
But they didn't know why UILabel was going to be somethings like error. They thought that was maybe because of UILabel's intrinsicContentSize .
But I still clearly don't know why was happened.
I would appreciate you if you give me clear answer. thanks!
here is my entire code.
you can run exactly just copy and paste.
Code Block swift import UIKit class ViewController3 : UIViewController { var greenLabel = UILabel() var redLabel = UILabel() var button = UIButton(type : .custom) var buttonConstraints : NSLayoutConstraint? var isClicked = false override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .white self.view.addSubview(greenLabel) self.view.addSubview(redLabel ) self.view.addSubview(button) button.backgroundColor = .blue redLabel.backgroundColor = .red greenLabel.backgroundColor = .green button.setTitle("I'm Button", for: .normal) button.addTarget(self, action: #selector(click), for: .touchUpInside) redLabel.translatesAutoresizingMaskIntoConstraints = false greenLabel.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false buttonConstraints = button.heightAnchor.constraint(equalToConstant: 50) NSLayoutConstraint.activate([ buttonConstraints!, greenLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100), greenLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10), greenLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10), greenLabel.heightAnchor.constraint(equalToConstant: 50), button.topAnchor.constraint(equalTo: self.greenLabel.bottomAnchor, constant: 50), button.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10), button.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10), redLabel.topAnchor.constraint(equalTo: self.button.bottomAnchor, constant: 50), redLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10), redLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10), redLabel.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), ]) } @objc func click() { switch isClicked { case true : UIView.animate(withDuration: 0.5, delay: 0.0, animations: { self.buttonConstraints?.constant = 50 self.view.layoutIfNeeded() }, completion: { complete in self.isClicked = false }) case false : UIView.animate(withDuration: 0.5, delay: 0.0, animations: { self.buttonConstraints?.constant = 200 self.view.layoutIfNeeded() }, completion : { complete in self.isClicked = true }) } } }