【问题标题】:Customizing NavigationBar per XLPagerTabStrip ButtonBarPagerTabStripViewController page自定义每个 XLPagerTabStrip ButtonBarPagerTabStripViewController 页面的 NavigationBar
【发布时间】:2021-10-16 13:24:33
【问题描述】:

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

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

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

有办法吗?

【问题讨论】:

    标签: ios swift xlpagertabstrip


    【解决方案1】:

    ButtonBarPagerTabStripViewController 类有一个覆盖函数来处理这个问题。

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

    输出:

    【讨论】:

    • 这不起作用。当我在示例中为您提供 if (print 1) 和 else 子句 (print 2) 的 print 语句时,我得到 11111111111 和 2222221111 ....我认为这不是正确的使用方法。我只想添加条形按钮并能够与 viewcontroller1 或 2 中的这些按钮进行交互
    • @infiniteObj 我在上面的答案中添加了示例项目代码。请看一下如何使用该功能。
    • 感谢这对 UI 端有用。你知道如何连接两个不同视图控制器的按钮操作吗?
    • 您能否更新一下我们如何应用来自不同 VC 的操作? @Darshan
    • @infiniteObj 我已经更新了按钮操作的代码。请查看更新后的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多