Akshaykumar Maldhure
Akshaykumar Maldhure

Reputation: 1259

Unable to connect XLPagertabStrip outlets using BaseButtonBarPagerTabStripViewController with custom cell

I wanted to create custom tabs using XLPagerTabStrip, I m using BaseButtonBarPagerTabStripViewController with custom collectionview cell. But the Problem I m getting is, I m not able to connect Buttonbarview and containerview from my class, can you please help me out. Bellow is my class

    import Foundation
import XLPagerTabStrip

class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<YoutubeIconCell> {

    let redColor = UIColor(red: 221/255.0, green: 0/255.0, blue: 19/255.0, alpha: 1.0)
    let unselectedIconColor = UIColor(red: 73/255.0, green: 8/255.0, blue: 10/255.0, alpha: 1.0)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "YoutubeIconCell", bundle: Bundle(for: YoutubeIconCell.self), width: { (cell: IndicatorInfo) -> CGFloat in
                return 55.0
        })
    }

    override func viewDidLoad() {
        // change selected bar color

        settings.style.buttonBarBackgroundColor = redColor
        settings.style.buttonBarItemBackgroundColor = .clear
        settings.style.selectedBarBackgroundColor = UIColor(red: 234/255.0, green: 234/255.0, blue: 234/255.0, alpha: 1.0)
        settings.style.selectedBarHeight = 4.0
        settings.style.buttonBarMinimumLineSpacing = 0
        settings.style.buttonBarItemTitleColor = .black
        settings.style.buttonBarItemsShouldFillAvailableWidth = true
        settings.style.buttonBarLeftContentInset = 0
        settings.style.buttonBarRightContentInset = 0



        changeCurrentIndexProgressive = { [weak self] (oldCell: YoutubeIconCell?, newCell: YoutubeIconCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
            guard changeCurrentIndex == true else { return }
            oldCell?.iconImage.tintColor = self?.unselectedIconColor
            newCell?.iconImage.tintColor = .white

        }
        super.viewDidLoad()
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    }

    // MARK: - PagerTabStripDataSource

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        let child_1 = FeedsTableViewController(style: .plain, itemInfo: IndicatorInfo(title: " HOME", image: UIImage(named: "c1")))
        let child_2 = FeedsTableViewController(style: .plain, itemInfo: IndicatorInfo(title: " TRENDING", image: UIImage(named: "d1")))
        return [child_1, child_2]
    }


    override func configure(cell: YoutubeIconCell, for indicatorInfo: IndicatorInfo) {
        cell.iconImage.image = indicatorInfo.image?.withRenderingMode(.alwaysTemplate)
    }

    override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
        super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
        if indexWasChanged && toIndex > -1 && toIndex < viewControllers.count {
            let child = viewControllers[toIndex] as! IndicatorInfoProvider
            UIView.performWithoutAnimation({ [weak self] () -> Void in
                guard let me = self else { return }
                me.navigationItem.leftBarButtonItem?.title =  child.indicatorInfo(for: me).title
            })
        }
    }

    // MARK: - Actions

    @IBAction func closeAction(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
}

Upvotes: 0

Views: 760

Answers (2)

hell2809
hell2809

Reputation: 35

I don't know about author's solution because I haven't tried it yet, but my solution (just tried it and successed) is:

  • Comment out all of your code for class that inherit from BaseButtonBarPagerTabStripViewController

  • Write new class with same name, inherit from ButtonBarPagerTabStripViewController (NOT THE BASE ONE, THIS IS WHERE THE MAGIC HAPPENS).

  • Go back to your storyboard/xib file, and now you can connect outlets with containerView and buttonBarView.

  • After that, undo what you did with your class.

Upvotes: 1

Akshaykumar Maldhure
Akshaykumar Maldhure

Reputation: 1259

Finally I got the answer to this, and I m posting it now, In case of XLPagerTabStrip it doesnt work when we set initial viewcontroller from storyboard. It only works when we start it programmatically, thats the only mistake I was making when using this library. Thanks!

     let _ = YoutubeExampleViewController(nibName: nil, bundle: nil)
     let sb = UIStoryboard(name: "Main", bundle: nil)
     let tabBarViewController = sb.instantiateViewController(withIdentifier: "YoutubeExampleViewController")
     let nav = UINavigationController(rootViewController: tabBarViewController)
     window = UIWindow(frame: UIScreen.main.bounds)
     window?.rootViewController = nav
     window?.makeKeyAndVisible()

Upvotes: 0

Related Questions