【问题标题】:Reload UITabBarController on demand按需重新加载 UITabBarController
【发布时间】:2020-01-01 00:20:28
【问题描述】:

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

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

函数调用正确,问题是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()

【问题讨论】:

    标签: ios swift uiviewcontroller uitabbarcontroller swift5


    【解决方案1】:

    我觉得我们不需要再次创建 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”值。

    【讨论】:

    • 您能否详细说明您在上述代码中遇到的问题。
    • 图片没有更新。
    【解决方案2】:

    您可能需要将图像的渲染模式设置为UIImageRenderingModeAlwaysOriginal

    尝试改变这个:

      navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image1")!, by: scale)
    

    有了这个:

      navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image1")!, by: scale).withRenderingMode(.alwaysOriginal)
    

    编辑 - 示例代码

    考虑这个设置:

    • 初始视图控制器是一个自定义类 TabBarViewController
    • 红色背景视图控制器是一个 UIViewController,故事板 ID 为“First”
    • 橙色背景视图控制器是一个自定义类 SecondViewController,具有 IBAction 和情节提要 ID“Second”

    Assets.xcassets 文件包含三张图片(40x40 png):

    TabBarViewController

    import UIKit
    
    class TabBarViewController: UITabBarController {
    
        var something: Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupItems()
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
        }
    
        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")!.withRenderingMode(.alwaysOriginal)
    
            let secondVC = storyboard.instantiateViewController(withIdentifier: "Second")
            let navSecondController = UINavigationController(rootViewController: secondVC)
    
            navSecondController.tabBarItem.image = UIImage(named: "image2")!.withRenderingMode(.alwaysOriginal)
            navSecondController.tabBarItem.imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)
    
            if something == true {
                navFirstController.tabBarItem.image = UIImage(named: "image3")!.withRenderingMode(.alwaysOriginal)
            } else {
                navFirstController.tabBarItem.image = UIImage(named: "image1")!.withRenderingMode(.alwaysOriginal)
            }
    
            navFirstController.tabBarItem.imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)
    
            viewControllers = [navSecondController, navFirstController]
        }
    
    }
    

    SecondViewController

    import Foundation
    import UIKit
    
    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? TabBarViewController {
                tabVC.something = !tabVC.something
                tabVC.setupItems()
            }
        }
    
    }
    

    输出

    【讨论】:

    • 只在 image1 上还是在 image3 上?
    • 尝试在所有图像上更改此行为
    • 你可以试试不使用比例语法,像这样:UIImage(named: "image1")!.withRenderingMode(.alwaysOriginal) ?
    • 我认为这里的问题是我不必更改视图控制器。图像应该独立于当前视图控制器而改变。
    猜你喜欢
    • 2015-03-28
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多