1

I am using XLPagerTabStrip library and using the ButtonBarPagerTabStripViewController. This is what the hierarchy looks like: enter image description here

The ButtonBarPagerTabStripViewController is embedded inside a navigationController and I have two different viewControllers that the ButtonBarPagerTabStripViewController contains. The problem is that I want to change how the navigationBar looks based one either ViewController1 or ViewController2.

For example, when the tab is one viewController1, I want to have a barButton "+". For viewController2, I want to have a barButton of report. However, I cannot make modifications to the navigationController bar from viewController1 and viewController2.

Is there a way?

1 Answers1

1

ButtonBarPagerTabStripViewController class has a override function to handle this.

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

Add condition as per your total number of view controllers on the screen.

Update

Please see the below code used in the demo project.

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

}

Output:

enter image description here

Darshan
  • 431
  • 4
  • 12
  • This doesn't work. When I put print statement for both you if (print 1) and else clause (print 2) in the example, I get 11111111111 and then 2222221111....I don't think its quite the correct method to use. I just want to add bar buttons and be able to interact with those buttons in either viewcontroller1 or 2 – infiniteObj Aug 13 '21 at 10:07
  • @infiniteObj I have added the sample project code in the above answer. Please have a look at it how to use the function. – Darshan Aug 13 '21 at 10:48
  • Thanks this worked for the UI side. Do you know how I can connect the actions of the buttons for the two different view controllers? – infiniteObj Aug 14 '21 at 05:56
  • Could you update on how we can apply the actions from different VCs? @Darshan – infiniteObj Aug 15 '21 at 01:13
  • @infiniteObj Sure, I’ll update the answer. – Darshan Aug 15 '21 at 03:39
  • I think your code also breaks the scrolling behaviour of the tab bar. When you scroll using the swipe gesture, it doesn't move the scroll indicator for the tabs. – infiniteObj Aug 15 '21 at 03:53
  • 1
    @infiniteObj I have updated the code for button action. Please have a look at the updated answer. – Darshan Aug 15 '21 at 07:20
  • @infiniteObj for scrolling issue of the tab bar while swipe gesture you can check the library open issues. Because I am using the same library and I have disabled the swipe gesture. – Darshan Aug 15 '21 at 07:28
  • How do you disable the swipe? – infiniteObj Aug 15 '21 at 07:35
  • `containerView.isScrollEnabled = false` just add this code in `viewDidLoad()` function. It will disable the swipe gesture in the tab bar. – Darshan Aug 15 '21 at 08:06