[CocoaPods] How to Use TabMan

Tabman
https://github.com/uias/Tabman 



Super easy to implement page view controller with indicator bar slide controllers Tabman

Install pod :

pod 'Tabman'


how to use :

1. Use simultaneously PageBoy and Tabman


import Pageboy
import Tabman
simple use

class YourTabViewController: TabmanViewController,  PageboyViewControllerDataSource
  var viewControllers: [UIViewController] = [
        ViewController1(),
        ViewController2(),
    ] 
override func viewDidLoad() {
  super.viewDidLoad()

  self.dataSource = self

         // configure the bar
         self.bar.items = [Item(title: "Page 1"),
                             Item(title: "Page 2")]
 }
}
  func numberOfViewControllers(in pageboyViewController: PageboyViewController) -> Int {
        return self.viewControllers.count
    }
    
    func viewController(for pageboyViewController: PageboyViewController, at index: PageboyViewController.PageIndex) -> UIViewController? {
        return self.viewControllers[index]
    }
    
    func defaultPage(for pageboyViewController: PageboyViewController) ->
        PageboyViewController.Page? {
        return nil
    }




2. Full Code swift file

import Pageboy
import Tabman
import UIKit

class YourTabViewController: TabmanViewController {
    var viewControllers: [UIViewController] = [
        ViewController1(),
        ViewController2(),
        ViewController3(),
        ViewController4(),
        ViewController5(),
        ViewController6()
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.backBarButtonItem?.tintColor = #colorLiteral(red: 0.5704585314, green: 0.5704723597, blue: 0.5704649091, alpha: 1)
        self.title = "Cari Asuransi"
        self.dataSource = self
        
        self.bar.items = [
            Item(title: "Item1"),
            Item(title: "Item2"),
            Item(title: "Item3"),
            Item(title: "Item4"),
            Item(title: "Item5"),
            Item(title: "Item6")
        ]
        
        self.bar.style = . scrollingButtonBar  
        self.bar.appearance = TabmanBar.Appearance({ appearance in
            
            appearance.indicator.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
            appearance.indicator.lineWeight = .normal
            appearance.indicator.compresses = true
            
            appearance.text.font = UIFont.systemFont(ofSize: 12.0, weight: .medium)
            
            appearance.layout.height = .explicit(value: 5.0)
            appearance.layout.interItemSpacing = 15.0
            
        })
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        navigationController?.navigationBar.barTintColor = UIColor.green
        self.doSomething()
    }
    
}

extension YourTabViewController: PageboyViewControllerDataSource {
    func numberOfViewControllers(in pageboyViewController: PageboyViewController) -> Int {
        return self.viewControllers.count
    }
    
    func viewController(for pageboyViewController: PageboyViewController, at index: PageboyViewController.PageIndex) -> UIViewController? {
        return self.viewControllers[index]
    }
    
    func defaultPage(for pageboyViewController: PageboyViewController) ->
        PageboyViewController.Page? {
        return nil
    }
}

Update :

if you want use collectionview click per cell (collectionviewcell) in firstViewController and in secondController use Tabman add this in your program :

- In FirstController

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.colectinView1?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
        
if collectionView == self.colectionView {
            let vc1 = YourTabViewController()
            
            switch indexPath.row {
            case 0:
                self.navigationController?.pushViewController(vc1, animated: true)
            case 1:
                self.navigationController?.pushViewController(vc1, animated: true)
                vc1.itemSelectIndex = 1
            case 2:
                self.navigationController?.pushViewController(vc1, animated: true)
                vc1.itemSelectIndex = 2
            case 3:
                self.navigationController?.pushViewController(vc1, animated: true)
                vc1.itemSelectIndex = 3
                
            default:
                break
            }
            return }
}


 - add this code In your Tabman (secondController)

 var itemSelectIndex = Int()
 override func viewDidLoad() {
        super.viewDidLoad()
        
        doSomething()        
        ....
        
        }
func doSomething() {
        if self.itemSelectIndex == 1 {
            scrollToPage(.at(index: 1), animated: true)
        }
        if self.itemSelectIndex == 2 {
            scrollToPage(.at(index: 2), animated: true)
        }
        if self.itemSelectIndex == 3 {
            scrollToPage(.at(index: 3), animated: true)
        }
    }










No comments: