我有以下带有2个项目的TabBarController。它显示正确。

setupItems()更改其值时,我正在从另一个 Controller 调用something函数。

该函数已正确调用,问题是没有更新navFirstController.tabBarItem.image

class TabBarController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
    setupItems()
  }

  func setupItems() {
    let scale: CGFloat = 0.35
    let navFirstController = UINavigationController(rootViewController: FirstController())

    let navSecondController = UINavigationController(rootViewController: SecondController())
    navSecondController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image2")!, by: scale)
    navSecondController.tabBarItem.imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)

    if something == true {
      navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image1")!, by: scale)
    } else {
      navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image3")!, by: scale)
    }

    navFirstController.tabBarItem.imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)

    viewControllers = [navSecondController, navFirstController]
  }

}


我尝试过:

1)viewControllers?.remove(at: 1)开头的setupItems()
2)navFirstController.removeFromParent()开头的setupItems()
3)self.viewWillLayoutSubviews()末尾的setupItems()
4)self.view.setNeedsLayout()self.view.setNeedsDisplay()结尾的setupItems()

最佳答案

我觉得我们不需要再次创建viewControllers对象来更改标签栏图像。
只是我们需要从viewControllers数组获取viewController对象并更改图像。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnClicked(_ sender: Any) {
        //change image of tab bar item on button clicked
        if let tabVC = self.tabBarController as? TabBarController {
            tabVC.changeImage()
        }
    }

}


class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupItems()
    }

    func setupItems() {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let firstVC = storyboard.instantiateViewController(withIdentifier: "First")
        let navFirstController = UINavigationController(rootViewController: firstVC)
        navFirstController.tabBarItem.image = UIImage(named: "Image1")

        let secondVC = storyboard.instantiateViewController(withIdentifier: "Second")
        let navSecondController = UINavigationController(rootViewController: secondVC)
        navSecondController.tabBarItem.image = UIImage(named: "Image2")

        viewControllers = [navSecondController, navFirstController]
    }

    func changeImage() {
        if let second = viewControllers?[1] as? UINavigationController {
            second.tabBarItem.selectedImage = UIImage(named: "Image3")
            second.tabBarItem.image = UIImage(named: "Image3")
        }
    }

}

注意如果要更改选定的选项卡栏项目图像,然后更改“selectedImage”值,否则更改“image”值。

关于ios - 按需重新加载UITabBarController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57681102/

10-12 17:28