Home > other >  Change constraints for a button on another screen Swift
Change constraints for a button on another screen Swift

Time:10-07

On the first and second screen I have 2 buttons, but on the third screen I should leave only one button, change the title for it and put it down, closer to the bottom of the view. Can’t figure out how to change button’s position if its constraints goes from storyboard. OrangeButton is above, whiteButton is under it. They are arranged vertically. I use 3 instances of the same VC to show these buttons. On the 3rd instance I try to hide whiteButton which is under orange one, and orange Button should go down.

            if index == 2 {
                whiteButton.isHidden = true
                orangeButton.setTitle("Start”, for: .normal)
                //here I have to set new constraints for orangeButton
            } else {
                whiteButton.isHidden = false
                orangeButton.setTitle("Next”, for: .normal)
            }



CodePudding user response:

If what you want to do is adjust the spacing to be even with different numbers of buttons, stack views are awesome. Put your buttons in a stack view. Set up the spacing between items to the desired amount. Hide the ones you don't want and the stack view shifts the remaining items to space them out correcly.

If that's not what you're asking update your question to describe what you are trying to do.

CodePudding user response:

You can do this by removing previous constraints.

if index == 2 {
     let VC=yourViewControlarClass()
     VC.whiteButton.isHidden = true
     VC.orangeButton.removeFromSuperView()
     VC.orangeButton.frame = CGrect(x: 100, y: 600, width: 200, height: 60)
     orangeButton.setTitle("Start”, for: .normal)
     VC.view.addSubView(orangeButton)
     self.present(VC, animated: true, completion: nil)
     
} else {
     let VC=yourViewControlarClass()
     VC.whiteButton.isHidden = false
     VC.orangeButton.setTitle("Next”, for: .normal)
     self.present(VC, animated: true, completion: nil)
}
  • Related