2

I'm trying to do a swipe navigation between different tableViewControllers in swift. I managed to do it with viewControllers with the following code :

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

}

var scrollViewAdded = false

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    if !scrollViewAdded {

        self.loadSrollView()

        self.scrollViewAdded = true
    }
}


func loadSrollView() {

    self.scrollView.pagingEnabled = true

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

    self.addChildViewController(vc0!)
    self.scrollView.addSubview(vc0!.view)
    vc0!.didMoveToParentViewController(self)


    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.scrollView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)


    let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

    var frame2 = vc2!.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2!.view.frame = frame2

    self.addChildViewController(vc2!)
    self.scrollView.addSubview(vc2!.view)
    vc2!.didMoveToParentViewController(self)


    let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

    var frame3 = vc3!.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc3!.view.frame = frame3

    self.addChildViewController(vc3!)
    self.scrollView.addSubview(vc3!.view)
    vc3!.didMoveToParentViewController(self)


    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}

}

I thought that I could adapt it by replacing the viewcontrollers by tableviewcontrollers but it doesn't work. Anyone knows how to do it?

Meet
  • 1,196
  • 12
  • 26
TheoF
  • 795
  • 2
  • 7
  • 16
  • By swipe navigation, do you mean for a side drawer ? – Meet Jul 29 '16 at 12:58
  • Something like this : https://www.youtube.com/watch?v=3jAlg5BnYUU but with TableViewControllers instead of ViewControllers. – TheoF Jul 29 '16 at 13:27
  • 1
    Yup, use XLPagerTabStrip :- https://github.com/xmartlabs/XLPagerTabStrip – Meet Jul 29 '16 at 13:34
  • Do you know about container views? To implement using that library you need to use container view. If you come across any issues using that library, let me know. i have used it few times. – Meet Jul 29 '16 at 13:36
  • Thanks, I'm going to try this. – TheoF Jul 29 '16 at 13:43
  • Just to know, isn't it possible to do it modifying the code that I posted above? – TheoF Jul 29 '16 at 13:43
  • Yup its possible. But would have to implement a demo project n debug view hierarchy for better understanding the issue. – Meet Jul 29 '16 at 13:47
  • Well I think I understand why it doesn't work : I can't simply replace the ViewController elements in the code by TableViewController as it won't work but I don't know how to do it... – TheoF Jul 29 '16 at 13:50
  • @MeetShah I installed the library, it seems that I have to custom the TableViewControllers programatically, the modifications that I want to do are a bit complex, I would need to do them from the storyboard. Can I do it from the library or is it better to modify the code that I posted above? – TheoF Jul 29 '16 at 14:38
  • can you clarify bit more on modifications that you what to make ? The way i see it there is no need to custom TableViewControllers. Let me give you e.g. of what exactly needs to be done w.r.t. XLPagerTabStrip Library - refer my answer below. – Meet Jul 30 '16 at 09:56

1 Answers1

6

Following are the steps to use XLPagerTabStrip Library in your project :

Step 1: Drag a VC on Storyboard (Supposedly Intial VC) and drag a Container View on this VC(fully occupying the VC). Drag another empty VC (define its class as "myPagerStrip" which will be created in next step). Now Control drag from Container View and choose embed and define identifier of this Storyboard Embed segue as "showMyPagerStrip"

Step 2: Create a new Swift file named "myPagerStrip.swift" with following code :-

Note: Any doubt with below code, refer the github link already mentioned above, everything is explained there.

import XLPagerTabStrip

class myPagerStrip: ButtonBarPagerTabStripViewController {


    override func viewDidLoad() {


        settings.style.buttonBarBackgroundColor = UIColor.yellowColor()


        // selected bar view is created programmatically so it's important to set up the following 2 properties properly
        settings.style.selectedBarBackgroundColor = UIColor.blackColor()
        //settings.style.selectedBarHeight: CGFloat = 5

        // each buttonBar item is a UICollectionView cell of type ButtonBarViewCell
        settings.style.buttonBarItemBackgroundColor =  UIColor.redColor()
        settings.style.buttonBarItemFont = UIFont.systemFontOfSize(18)
        // helps to determine the cell width, it represent the space before and after the title label
       // settings.style.buttonBarItemLeftRightMargin: CGFloat = 8
        settings.style.buttonBarItemTitleColor =  UIColor.whiteColor()
        // in case the barView items do not fill the screen width this property stretch the cells to fill the screen
        settings.style.buttonBarItemsShouldFillAvailiableWidth = true

        changeCurrentIndexProgressive = { (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
            guard changeCurrentIndex == true else { return }

            oldCell?.label.textColor = UIColor(white: 1, alpha: 0.6)
            newCell?.label.textColor = .whiteColor()

            if animated {
                UIView.animateWithDuration(0.1, animations: { () -> Void in
                    newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0)
                    oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8)
                })
            }
            else {
                newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0)
                oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8)
            }
        }

        super.viewDidLoad()
    }


    override func viewControllersForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {


        let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewOne") // First Table View

        let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewTwo") // Second Table View

        let child_3 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewThree") // Third Table View

        let childVC = [child_1,child_2, child_3]

        return childVC

    }

    override func reloadPagerTabStripView() {
        super.reloadPagerTabStripView()
    }

}

Step 3: Drag 3 more VC on Storyboard and create 3 Cocoa Class files namely tableViewOne.swift, tableViewTwo.swift and tableViewThree.swift with Storyboard IDs as "tableViewOne","tableViewTwo", "tableViewThree".//(As mentioned in above code for child_1,child_2 and child_3)

Note: Assuming all 3 tableViews or required tableViews are setup with required data.

Step 4: Inherit "IndicatorInfoProvider" in each of the tableViews (perform after step 5) i.e.

class tableViewOne: UIViewController,IndicatorInfoProvider

Step 5: Import XLPagerTabStrip in each of this files and also add following function in each tableView:-

func indicatorInfoForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: "My Child title 1")
    }

Now, you are done. Simply Run the project in Simulator or Actual device.:)

In case having trouble with table Views, following is code for tableViewOne.swift for reference :-

import UIKit
import XLPagerTabStrip

class tableViewOne: UIViewController,IndicatorInfoProvider {

    @IBOutlet var tableView: UITableView!
        var dummyData = ["data 0","data 001","data try"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func indicatorInfoForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: "My Child title 1")
    }

}
extension tableViewOne: UITableViewDataSource, UITableViewDelegate {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return dummyData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell

        cell.textLabel!.text = dummyData[indexPath.row]

        return cell;
    }

}
Meet
  • 1,196
  • 12
  • 26
  • Thanks for this very clear explanation, just a question : this will set 3 VC with tableviews inside, in my preceding trials the content of the tableview cells didn't appear when I launched the app, I needed to do it in tableViewControllers, is it possible to do what you just explained to me with tableViewControllers instead of VCs? – TheoF Jul 30 '16 at 12:43
  • Actually I could already do what you suggested in your solution (swipe between VCs with tableViews inside) but I would like to swipe between TableViewControllers instead, is it possible? – TheoF Jul 30 '16 at 12:45
  • Frankly speaking, Its better to use UITableView inside VC instead of UITableViewControllers, but its up to you. In either case it will work. I think you might have forgotten one of 2 things:- a) setup datasource and delegate from storyboard or b) identifier of cell might be incorrectly specfied. Check this in your project. – Meet Jul 30 '16 at 14:04
  • Implement as mentioned above, let me know in case you run into issues. After everything works out , don't forget to mark answer as accepted.Good luck for implementation. – Meet Jul 30 '16 at 14:06
  • Sure, I will, thank you very much, just trying to make it work before marking it as accepted in case I need more help. Thanks again for your time. – TheoF Jul 30 '16 at 14:30
  • I need to customize it a lot and it's really difficult to do it programatically whereas it's easy in the storyboard, that's why I want to use UITableViewControllers instead of UIViewControllers, how can I do it with UITableViewControllers? The code above won't work as I can't put InstantiateViewControllers with UITableViewControllers... – TheoF Jul 30 '16 at 16:17
  • Well the part where you said and i quote " it's really difficult to do it programatically whereas it's easy in the storyboard", I dont quite understand what exactly you mean by that ?. As same amount of coding needs to be done in either case for both UITableViewControllers or UITableView within UIViewController. As mentioned previouly what exactly you need to customize ?, if you mean prototype cells then same will be needed in both cases again. So kindly specify your problem. – Meet Jul 31 '16 at 07:34
  • Well I manage to make nicer customization from the storyboard with as little code as possible with UITableViewControllers whereas I don't with UIViewControllers including tableViews inside, I have to do it fully programatically, which is much harder for me. So if you have a solution to do this with UITableViewControllers instead of UIViewControllers I would gladly hear it. – TheoF Jul 31 '16 at 07:58
  • Ok. There will no changes w.r.t. Instantiation as UITableVC are still VC. So same line of code will work even for UITableVC. So is it working as expected? – Meet Jul 31 '16 at 10:19
  • I am just adding XLPagerTabStrip as a tag in your question. Hope you dont mind. – Meet Jul 31 '16 at 12:24
  • No problem, go ahead – TheoF Jul 31 '16 at 13:44