Swift - Segarkan Bilah UITabBarController Kustom

Jan 08 2021

Saya memiliki TabBar khusus, dengan tombol tengah terangkat, di atas TabBar. Ketika pengguna belum menyelesaikan tugas harian, setupIncompleteMiddleButton () akan muncul, menunjukkan itu. Namun, setelah pengguna menyelesaikan tugas, saya ingin setupCompleteMiddleButton () muncul, menunjukkan bahwa tugas telah diselesaikan. Saya tidak tahu bagaimana melakukan ini - saya seharusnya tidak memanggil viewDidLoad () di pengontrol dan ketika memanggilnya, itu tidak melakukan apa pun untuk menyegarkan tampilan. Menyegarkan TabBar tidak melakukan apa-apa.

Ini adalah pengontrol TabBar saya:

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  
}
}

Terima kasih!

Jawaban

1 yankitPatel Jan 08 2021 at 17:11

Anda dapat mencoba ini untuk objek tombol tunggal secara global dan hanya mengubah gambar setiap kali tugas selesai atau belum.

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

Mungkin Anda bisa mencoba ini:

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
    }
}