根据 XLPagerTabStrip ButtonBarPagerTabStripViewController 页面自定义 NavigationBar

Customizing NavigationBar per XLPagerTabStrip ButtonBarPagerTabStripViewController page

我正在使用 XLPagerTabStrip 库并使用 ButtonBarPagerTabStripViewController。这是层次结构的样子:

ButtonBarPagerTabStripViewController 嵌入在 navigationController 中,我有两个不同的 viewControllers ButtonBarPagerTabStripViewController 包含。问题是我想根据 ViewController1 或 ViewController2 更改导航栏的外观。

比如当tab是一个viewController1的时候,我想要一个barButton“+”。对于 viewController2,我想要一个报告的 barButton。但是,我无法从 viewController1 和 viewController2 修改 navigationController 栏。

有办法吗?

ButtonBarPagerTabStripViewController class 有一个覆盖函数来处理这个。

override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
    if progressPercentage == 1 {
        // Add `+` button to navigation item
    } else {
        // Add `report` button to navigation item
    }
}

根据屏幕上的视图控制器总数添加条件。

更新

请查看演示项目中使用的以下代码。

import UIKit
import XLPagerTabStrip

class MainTabBarViewController: ButtonBarPagerTabStripViewController {

    var isFirstView: Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func getSegmentList() -> [UIViewController] {
        let firstViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "FirstViewController")
        firstViewController.title = "First"
        let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "SecondViewController")
        secondViewController.title = "Second"
        return [firstViewController, secondViewController]
    }
    
    func setUI() {
        let addImage = UIImage(systemName: "plus")
        let addBarButton = UIBarButtonItem(image: addImage, style: .plain, target: self, action: #selector(addButtonAction(_:)))
        let reportImage = UIImage(systemName: "book")
        let reportBarButton = UIBarButtonItem(image: reportImage, style: .plain, target: self, action: #selector(reportButtonAction(_:)))
        navigationItem.rightBarButtonItem = isFirstView ? addBarButton : reportBarButton
    }
    
    @IBAction func addButtonAction(_ sender: UIBarButtonItem) {
        print("Add button action")
        // Add button action
    }
    
    @IBAction func reportButtonAction(_ sender: UIBarButtonItem) {
        print("Report button action")
        // Report button action
    }
    
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        return getSegmentList()
    }
    
    override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
        if progressPercentage == 1 {
            let viewController = viewController.viewControllers[toIndex] as? FirstViewController
            isFirstView = viewController?.title == "First"
            setUI()
        }
    }

}

输出: