Swift-カスタムUITabBarControllerバーを更新

Jan 08 2021

TabBarの上に中央のボタンが浮き上がったカスタムTabBarがあります。ユーザーが毎日のタスクを完了していない場合は、setupIncompleteMiddleButton()が表示され、そのことを示します。ただし、ユーザーがタスクを完了すると、setupCompleteMiddleButton()が表示され、タスクが完了したことが示されます。これを行う方法がわかりません-コントローラーで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
    }
}