0

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.

Takarii
  • 1,612
  • 2
  • 18
  • 29
sulabh qg
  • 1,155
  • 2
  • 12
  • 20

2 Answers2

1

NOTE: This version doesn't use constraints applied programmatically, but gets to the desired result!


My Main.storyboard File looks like this:

Main.storyboard


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:

Simulator Final Result


Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
0

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.

Community
  • 1
  • 1
Matteo Crippa
  • 121
  • 3
  • 10