marwen1609
2/17/2019 - 8:37 PM

Header TabBar for iOS

you need to creat a specific scrollView with twice the width of the current view and put a container view to load the new viewControllers

//
//  InvestmentFundesViewController.swift
//  Intermedium
//
//  Created by Marwen Jamel on 20/09/18.
//  Copyright © 2018 iOasys. All rights reserved.
//

import UIKit
import Intercom

protocol InvestmentFundesViewControllerDelegate: class {
    func didSelect(menuOption: InvestmentHomeItem, on viewController: InvestmentFundesViewController)
}
class InvestmentFundesViewController: MenuChildViewController {
    
      // MARK: - IBOutlets
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var minhaCarteiraLabel: UILabel!
    @IBOutlet weak var queroInvestirLabel: UILabel!
    
    @IBOutlet weak var minhaCarteiraContainer: UIView!
    @IBOutlet weak var queroInvestirContainer: UIView!
    @IBOutlet weak var scrollIndicatorLeftConstraint: NSLayoutConstraint!
    
      // MARK: - Variables 
    var accountInfo: AccountInfo!
    var isIwantToInvestSelected: Bool?
    weak var delegate: InvestmentFundesViewControllerDelegate?
    
      // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isIwantToInvestSelected == false {
            DispatchQueue.main.async {
                self.scrollView.setContentOffset(CGPoint(x: self.view.frame.size.width * CGFloat(0), y: 0), animated: true)
                self.activeLabel(label: self.minhaCarteiraLabel)
                self.deactivatesLabel(label: self.queroInvestirLabel)
            }
           
        }else {
        DispatchQueue.main.async {
            self.scrollView.setContentOffset(CGPoint(x: self.view.frame.size.width * CGFloat(1), y: 0), animated: true)
            self.activeLabel(label: self.queroInvestirLabel)
            self.deactivatesLabel(label: self.minhaCarteiraLabel)
            }
        }
        
    }
    @IBAction func didTapMinhaCarteira(_ sender: Any) {
        self.scrollView.setContentOffset(CGPoint(x: self.view.frame.size.width * CGFloat(0), y: 0), animated: true)
        activeLabel(label: minhaCarteiraLabel)
        deactivatesLabel(label: queroInvestirLabel)
        self.updateIntercomVisibility(visible: false)
    }
    
    @IBAction func didTapQueroInvestir(_ sender: Any) {
        self.scrollView.setContentOffset(CGPoint(x: self.view.frame.size.width * CGFloat(1), y: 0), animated: true)
        activeLabel(label: queroInvestirLabel)
        deactivatesLabel(label: minhaCarteiraLabel)
        self.updateIntercomVisibility(visible: true)
    }
    
    // MARK: - Navigation
    override func goBack() {
        self.updateIntercomVisibility(visible: false)
            _ = self.navigationController?.popViewController(animated: true)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "investmentStatment" {
            let controller = segue.destination as! InvestmentStatementViewController
            controller.delegate = (self as InvestmentStatementViewControllerDelegate)
            controller.accountInfo = self.accountInfo
            self.delegate?.setHomeViewController(viewController: controller)
        }else{
            let controller = segue.destination as! IwantToInvestViewController
            controller.accountInfo = self.accountInfo
            controller.delegate = self 
        }
        
    }
    
    
    func activeLabel(label:UILabel){
        label.textColor = Colors.orange
    }
    
    func deactivatesLabel(label:UILabel){
        label.textColor = #colorLiteral(red: 0.5490196078, green: 0.568627451, blue: 0.662745098, alpha: 1)
    }
    
}

// MARK: - UIScrollViewDelegate

extension InvestmentFundesViewController: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.scrollIndicatorLeftConstraint.constant = scrollView.contentOffset.x / 2
        
        print("valor quando entrou: " + String(describing: scrollView.contentOffset.x / self.view.frame.size.width))
        
        if scrollView.contentOffset.x / self.view.frame.size.width < 1.0 {
            print("First View")
            
            activeLabel(label: minhaCarteiraLabel)
            deactivatesLabel(label: queroInvestirLabel)
            
        }else if scrollView.contentOffset.x / self.view.frame.size.width < 2.0 {
            print("Second View")
            
            activeLabel(label: queroInvestirLabel)
            deactivatesLabel(label: minhaCarteiraLabel)
        }
        
        self.view.layoutIfNeeded()
    }
}