स्विफ्ट - कस्टम UITabBarController बार ताज़ा करें

Jan 08 2021

मेरे पास एक कस्टम टैबबार है, एक उठाए हुए मध्य बटन के साथ, टैबबार के ऊपर। जब उपयोगकर्ता ने एक दैनिक कार्य पूरा नहीं किया है, तो setupIncompleteMiddleButton () प्रदर्शित होना चाहिए, जो दर्शाता है। हालाँकि, एक बार जब उपयोगकर्ता कार्य पूरा कर लेता है, तो मैं चाहूंगा कि सेटअप पूर्ण हो जाए। मुझे नहीं पता कि यह कैसे करना है - मुझे नियंत्रक में viewDidLoad () को कॉल नहीं करना चाहिए और इसे कॉल करते समय, यह दृश्य को ताज़ा करने के लिए कुछ भी नहीं करता है। TabBar को ताज़ा करने से कुछ नहीं होता है।

यह मेरा 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  
}
}

धन्यवाद!

जवाब

1 yankitPatel Jan 08 2021 at 17:11

आप विश्व स्तर पर एकल बटन के लिए यह कोशिश कर सकते हैं और जब भी कार्य पूरा हो या न हो, केवल छवि बदलें।

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

शायद आप यह कोशिश कर सकते हैं:

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