Swift - Actualizar barra UITabBarController personalizada

Jan 08 2021

Tengo una TabBar personalizada, con un botón central elevado, sobre la TabBar. Cuando el usuario no ha completado una tarea diaria, debería aparecer setupIncompleteMiddleButton (), indicándolo. Sin embargo, una vez que el usuario completa la tarea, me gustaría que aparezca setupCompleteMiddleButton (), lo que indica que ha completado la tarea. No sé cómo hacer esto, no debería llamar a viewDidLoad () en el controlador y, al llamarlo, no hace nada para actualizar la vista. Actualizar TabBar no hace nada.

Este es mi controlador TabBar:

class TabBarController: UITabBarController, UITabBarControllerDelegate {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()

    // This is where I currently decide which button to show the "complete" one if the task is done, and the incomplete one if not. 
    self.delegate = self
    if UserData.hasCompletedDailyTask() == true {
        setupCompleteMiddleButton()
    } else {
        setupIncompleteMiddleButton()
    }
}

// Incomplete button 
func setupIncompleteMiddleButton() {
    let middleButton = UIButton(frame: CGRect(x: (self.view.bounds.width / 2) - 25, y: -20, width: 50, height: 50))
    
    middleButton.backgroundColor = UIColor.systemYellow
    middleButton.setImage(UIImage(systemName: "sun.max.fill"), for: .normal)
    middleButton.imageView?.tintColor = UIColor.white
    middleButton.layer.cornerRadius = middleButton.frame.width / 2
    middleButton.clipsToBounds = true
    
    self.tabBar.addSubview(middleButton)
    middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)
    self.view.layoutIfNeeded()
}

// Complete button
func setupCompleteMiddleButton() {
    let middleButton = UIButton(frame: CGRect(x: (self.view.bounds.width / 2) - 25, y: -20, width: 50, height: 50))

    middleButton.backgroundColor = UIColor.systemGreen
    middleButton.setImage(UIImage(systemName: "checkmark"), for: .normal)
    middleButton.imageView?.tintColor = UIColor.white
    middleButton.layer.cornerRadius = middleButton.frame.width / 2
    middleButton.clipsToBounds = true

    self.tabBar.addSubview(middleButton)
    middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)
    self.view.layoutIfNeeded()
}

@objc func middleButtonAction(sender: UIButton) {
    self.selectedIndex = 1  
}
}

¡Gracias!

Respuestas

1 yankitPatel Jan 08 2021 at 17:11

Puede probar esto para un objeto de botón globalmente único y simplemente cambiar la imagen siempre que la tarea esté completa o no.

class TabBarController: UITabBarController, UITabBarControllerDelegate {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

// TabbarController hode this button
var middleButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    setupButton()
    // This is where I currently decide which button to show the "complete" one if the task is done, and the incomplete one if not.
    self.delegate = self
    // taskCompletion is a call back when UserData finish its task
    if UserData.taskCompletion {
        setupCompleteMiddleButton()
    } else {
    setupIncompleteMiddleButton()
    }
}

func setupButton() {
    middleButton = UIButton(frame: CGRect(x: (self.view.bounds.width / 2) - 25, y: -20, width: 50, height: 50))
    middleButton.backgroundColor = UIColor.systemYellow
    middleButton.layer.cornerRadius = middleButton.frame.width / 2
    middleButton.clipsToBounds = true
    self.tabBar.addSubview(middleButton)
    middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)
    self.view.layoutIfNeeded()
}
//  Incomplete button
func setupIncompleteMiddleButton() {
  middleButton.backgroundColor = UIColor.systemYellow
    middleButton.setImage(UIImage(systemName: "sun.max.fill"), for: .normal)
    
}

// Complete button
func setupCompleteMiddleButton() {
    // change button color and image
    middleButton.backgroundColor = UIColor.systemGreen
    middleButton.setImage(UIImage(systemName: "checkmark"), for: .normal)
}

@objc func middleButtonAction(sender: UIButton) {
    self.selectedIndex = 1
}
EthanKwan Jan 08 2021 at 16:59

Quizás puedas probar esto:

import UIKit

class TabBarController: UITabBarController, UITabBarControllerDelegate {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    // TabbarController hode this button
    var middleButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // This is where I currently decide which button to show the "complete" one if the task is done, and the incomplete one if not.
        self.delegate = self
        // taskCompletion is a call back when UserData finish its task
        if UserData.taskCompletion {
            setupCompleteMiddleButton()
        }
        setupIncompleteMiddleButton()
    }
    
    //  Incomplete button 
    func setupIncompleteMiddleButton() {
        // initialize button  
        middleButton = UIButton(frame: CGRect(x: (self.view.bounds.width / 2) - 25, y: -20, width: 50, height: 50))

        middleButton.backgroundColor = UIColor.systemYellow
        middleButton.setImage(UIImage(systemName: "sun.max.fill"), for: .normal)
        middleButton.imageView?.tintColor = UIColor.white
        middleButton.layer.cornerRadius = middleButton.frame.width / 2
        middleButton.clipsToBounds = true
        self.tabBar.addSubview(middleButton)
        middleButton.addTarget(self, action: #selector(self.middleButtonAction), for: .touchUpInside)
        self.view.layoutIfNeeded()
    }
    
    // Complete button
    func setupCompleteMiddleButton() {
        // change button color and image
        middleButton.backgroundColor = UIColor.systemGreen
        middleButton.setImage(UIImage(systemName: "checkmark"), for: .normal)
    }
    
    @objc func middleButtonAction(sender: UIButton) {
        self.selectedIndex = 1
    }
}