skip to Main Content

I have 2 buttons. "Stocks" one is default, it has larger size and bold, the "Favorites" one is small. When the second one is pressed, I want "Stocks" to change size dynamically. I tried to hardcode, but then realized viewDidLoad() is called only once. How can I handle this?

//MARK: - Stocks Button and Favorites Button

        stocksButton.setTitle("Stocks",     for: .normal)
        activated(button: stocksButton)
        favoritesButton.setTitle("Favorites",  for: .normal)
        disabled(button: favoritesButton)
        
        if favoritesButton.isSelected {
            disabled(button: stocksButton)
            activated(button: favoritesButton)
        }
        if (stocksButton.isSelected) {
            disabled(button: favoritesButton)
            activated(button: stocksButton)
        }
        func disabled(button: UIButton) {
            button.setTitleColor(.gray, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        }
        func activated(button: UIButton) {
            button.setTitleColor(.black, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 28)
        }
// this is in my viewDidLoad() function

2

Answers


  1. You will need to do this in the methods that get called when each button is pressed. So in the (for example) stocksButtonPressed you can set disabled(button: favoritesButton) and in the favoritesButtonPressed you can call disabled(button: stocksButton).

    Login or Signup to reply.
  2. The is another way to do it. Declare your buttons, set constraints an use CGAffineTransform in UIview.animate:

    class yourControllerController: UIViewController {
    
    let saveButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .yellow
        button.setTitle("Button 2", for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold)
        button.setTitleColor(.black, for: .normal)
        button.layer.cornerRadius = 10
        button.layer.masksToBounds = true
        return button
    }()
    
    let saveButton2: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .black
        button.setTitle("Button 1", for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold)
        button.setTitleColor(.white, for: .normal)
        button.layer.cornerRadius = 10
        button.layer.masksToBounds = true
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add function to your button
        saveButton.addTarget(self, action: #selector(yourFunction), for: .touchUpInside)
        saveButton2.addTarget(self, action: #selector(yourFunction), for: .touchUpInside)
    
        view.backgroundColor = .red
        
        // set constraints
        view.addSubview(saveButton)
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        saveButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
        saveButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
        saveButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        saveButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
        view.addSubview(saveButton2)
        saveButton2.translatesAutoresizingMaskIntoConstraints = false
        saveButton2.bottomAnchor.constraint(equalTo: saveButton.topAnchor, constant: -30).isActive = true
        saveButton2.widthAnchor.constraint(equalToConstant: 150).isActive = true
        saveButton2.heightAnchor.constraint(equalToConstant: 60).isActive = true
        saveButton2.centerXAnchor.constraint(equalTo: saveButton.centerXAnchor).isActive = true
        
        // first state of small button
        let tarnsform = CGAffineTransform(scaleX: 0.7, y: 0.7)
        saveButton.transform = tarnsform
        saveButton.alpha = 0.5
        saveButton.isEnabled = true
        saveButton2.isEnabled = false
    }
    
    var controlButtons = 0 // set a value to control button state
    
    @objc fileprivate func yourFunction() {
        
        if self.controlButtons == 0 {
            UIView.animate(withDuration: 0.5, delay: 0) {
                let tarnsform = CGAffineTransform(scaleX: 0.7, y: 0.7)
                self.saveButton2.transform = tarnsform
                self.saveButton.transform = CGAffineTransform(scaleX: 1, y: 1)
                self.saveButton.alpha = 1
                self.saveButton2.alpha = 0.5 // after that you can set buttons title color
                
                self.saveButton.isEnabled = false
                self.saveButton2.isEnabled = true
                
                self.view.layoutIfNeeded()
                self.controlButtons = 1
            }
        } else {
            
            UIView.animate(withDuration: 0.5, delay: 0) {
                self.saveButton2.transform = CGAffineTransform(scaleX: 1, y: 1)
                let tarnsform2 = CGAffineTransform(scaleX: 0.7, y: 0.7)
                self.saveButton.transform = tarnsform2
                self.saveButton.alpha = 0.5
                self.saveButton2.alpha = 1 // after that you can set buttons title color
                
                self.saveButton.isEnabled = true
                self.saveButton2.isEnabled = false
                
                self.view.layoutIfNeeded()
                self.controlButtons = 0
            }
          }
        }
      }
    

    This is the result

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search