Search code examples
iosswiftuicollectionviewuipageviewcontroller

Swift : Swipe Both UICollectionView and UIPageViewController


I have created this interface with collectionView for tabs and pageViewController for swiping pages and am getting the index of page when i swipe but not able to change collectionViewTab how to change it? Anyone tell me, how to Update collectionViewTab bottom indicator???

 func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

  }

in this I am getting correct page index:

enter image description here


Solution

  • You are on the right path. When you swipe on UIPageViewController, you get current page index. You should be saving that index to class variable and use it in cellForItemAtIndexPath to determine if tab index matches currently displayed UIPageViewController. So something like this should work:

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("reuseIdentifier", forIndexPath: indexPath)
    
          if indexPath.item == classIndexVariable {
            bottomIndicator.isHidden = false // show indicator at current position
          }else{
            bottomIndicator.isHidden = true // hide indicator from all only shows in matched condition
           }
    
          return cell
        }
    

    To explain: In the cellForItemAtIndexPath you have to compare current cell index with index you saved in UIPageViewController didFinishAnimating function. If it matches you have active tab - show bottom indicator. If index doesn't match, you tab is not active so don't show bottom indicator.

    As I said, please keep in mind this is just a snipper. Don't know what you code looks like, I am guessing you are doing something like this.