I have three vertically aligned UIButton A,B,C in viewController. I want to add constrains programmatically in button C such that if i hide/remove button B then C should be in the place of B, if button B appears then C should be next to B. How can i add constrains programmatically.
-
I understand the first part but the second C should be next to B or under B?... – Tj3n Aug 09 '16 at 07:57
-
Is it good if I don't use constraints, but I get to the result needed? – Mr. Xcoder Aug 09 '16 at 08:17
-
I'm working on it. – Mr. Xcoder Aug 09 '16 at 08:17
-
Do you add the buttons programmatically?? If so, I cannot help you. If you add the buttons from the storyboard I will help you. – Mr. Xcoder Aug 09 '16 at 08:25
-
try using stackview and u can simply use button.hidden=true. it is the easiest way to achieve what you want. – George Aug 09 '16 at 08:31
-
i made all buttons programmatically, all buttons are vertically aligned if button B is present then my sequence is A-B-C, if button B is not present then A-C, that is what i'm trying to make. – sulabh qg Aug 09 '16 at 09:08
2 Answers
NOTE: This version doesn't use constraints applied programmatically, but gets to the desired result!
My Main.storyboard File
looks like this:
Explanation: I have 3 button outlets and one button action. When you press the hide Button B button, Button C
disappears, but Button B
receives the title Button C
and also its action, and hide Button B becomes show Button B. The actions of the buttons are Apressed()
, Bpressed()
and Cpressed()
. You can add your own code in there, I have just gave backgroundColor()
as an example.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var buttonA: UIButton!
@IBOutlet weak var buttonB: UIButton!
@IBOutlet weak var buttonC: UIButton!
var i = 0
override func viewDidLoad() {
super.viewDidLoad()
buttonA.addTarget(self, action: #selector(ViewController.Apressed), for: UIControlEvents.touchUpInside)
buttonB.addTarget(self, action: #selector(ViewController.Bpressed), for: UIControlEvents.touchUpInside)
buttonC.addTarget(self, action: #selector(ViewController.Cpressed), for: UIControlEvents.touchUpInside)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func hideB(_ sender: AnyObject) {
if i == 0{
buttonC.isHidden = true
buttonB.setTitle("Button C", for: .normal)
buttonB.removeTarget(self, action: #selector(ViewController.Bpressed), for: .touchUpInside)
buttonB.addTarget(self, action: #selector(ViewController.Cpressed), for: .touchUpInside)
i += 1
sender.setTitle("Show Button B", for: .normal)
}
else if i == 1{
buttonC.isHidden = false
buttonB.removeTarget(self, action: #selector(ViewController.Cpressed), for: .touchUpInside)
buttonB.addTarget(self, action: #selector(ViewController.Bpressed), for: UIControlEvents.touchUpInside)
buttonB.setTitle("Button B", for: .normal)
i = 0
sender.setTitle("Hide Button B", for: .normal)
}
}
func Apressed(){
self.view.backgroundColor = UIColor.red()
}
func Bpressed(){
self.view.backgroundColor = UIColor.green()
}
func Cpressed(){
self.view.backgroundColor = UIColor.yellow()
}
}
The Final Result:

- 4,719
- 5
- 26
- 44
I do suggest you to set first all the constraints via Storyboard (if possibile) of the 3 buttons and then set also a fixed height via constraints too.
Than you can create an @IBOutlet weak var bHeight: NSLayoutConstrait!
and attach it to your UIButton B Height
in storyboard ( you can see how to do that also directly from Storyboard from here ).
Then if you force bHeight.constant = 0
you should be able to achieve your goal.

- 1
- 1

- 121
- 3
- 10