antiguab / BATabBarController

A TabBarController with a unique animation for selection
https://antiguab.github.io/batabbarcontroller/
MIT License
1.06k stars 131 forks source link

Swift Support! #3

Closed Dershowitz011 closed 6 years ago

Dershowitz011 commented 8 years ago

Could you please add swift support to this? Or create a Swift branch? Thanks!

tran-duy-linh commented 8 years ago

I have used Swift. My code here:

import UIKit
import BATabBarController

var baTabBarController: BATabBarController!  

// MARK: - Private methods 
private func initViewControllers() {
    let vc1 = UIViewController()
    let vc2 = UIViewController()
    let vc3 = UIViewController()
    //
    let tabBarItem1 = BATabBarItem(image: UIImage(named: "ic_List_unselected"), selectedImage: UIImage(named: "ic_List"))
    let tabBarItem2 = BATabBarItem(image: UIImage(named: "ic_Request_unselected"), selectedImage: UIImage(named: "ic_Request"))
    let tabBarItem3 = BATabBarItem(image: UIImage(named: "ic_Profile_unselected"), selectedImage: UIImage(named: "ic_Profile"))

    baTabBarController = BATabBarController()
    baTabBarController.viewControllers = [vc1,vc2,vc3]
    baTabBarController.tabBarItems = [tabBarItem1,tabBarItem2,tabBarItem3]
    baTabBarController.delegate = self
    baTabBarController.tabBarBackgroundColor = UIColor.blackColor()
    baTabBarController.tabBarItemStrokeColor = UIColor.redColor()
    baTabBarController.setSelectedViewController(vc1, animated: true)
    self.view.addSubview(baTabBarController.view)
}
// MARK: - BATabBarControllerDelegate
func tabBarController(tabBarController: BATabBarController!, didSelectViewController viewController: UIViewController!) {
    print("Delegate success!")
}
arshbot commented 8 years ago

Is this all inside a class..? Or outside? Do we need to use initViewControllers?

MaeseppTarvo commented 8 years ago

Swift version would be awesome!

youngvz commented 8 years ago

Here is how I got the library to work nicely with Swift 3.0

To distinguish between your View Controllers in the didSelect method assign a title to the View Controller when instantiating in viewDidLayoutSubviews()

import UIKit
import BATabBarController

class TabBarViewController: UIViewController, BATabBarControllerDelegate {

    var tabBar: BATabBarController!

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

    let tabBarItem1: BATabBarItem = {
        let tabBarItem = BATabBarItem(image: UIImage(named: "alert_unselected")?.withRenderingMode(.alwaysTemplate), selectedImage: UIImage(named: "alert"))
        return tabBarItem!
    }()

    let tabBarItem2: BATabBarItem = {
        let tabBarItem = BATabBarItem(image: UIImage(named: "calendar_unselected")?.withRenderingMode(.alwaysTemplate), selectedImage: UIImage(named: "calendar"))
        tabBarItem?.tintColor = .blue

        return tabBarItem!
    }()
    let tabBarItem3: BATabBarItem = {
        let tabBarItem = BATabBarItem(image: UIImage(named: "map_unselected")?.withRenderingMode(.alwaysTemplate), selectedImage: UIImage(named: "map"))
        tabBarItem?.tintColor = .blue
        return tabBarItem!
    }()

    override func viewDidLayoutSubviews() {

        let vc1 = UINavigationController(rootViewController: announcementFeedController(collectionViewLayout: UICollectionViewFlowLayout()))
        vc1.title = "Announcements"
        let vc2 = UINavigationController(rootViewController: calendarController(collectionViewLayout: UICollectionViewFlowLayout()))
        vc2?.title = "Schedule"
        let vc3 =UINavigationController(rootViewController: mapController(collectionViewLayout: UICollectionViewFlowLayout()))
        vc3?.title = "Maps"

        self.tabBar = BATabBarController()
        self.tabBar.tabBarItemStrokeColor = .white;
        self.tabBar.viewControllers = [vc1, vc2!, vc3!]
        self.tabBar.tabBarItems = [tabBarItem1, tabBarItem2, tabBarItem3]
        self.tabBar.setSelectedView(vc1, animated: false)

        self.tabBar.tabBarBackgroundColor = UIColor(red:0.14, green:0.32, blue:0.95, alpha:1.00)
        self.tabBar.delegate = self
        self.view.addSubview(self.tabBar.view)

    }
    //Changes the tabBar Background color and the tint color of the tabBar icons

    func tabBarController(_ tabBarController: BATabBarController, didSelect viewController: UIViewController) {

        if let title = viewController.title{
            switch title {
            case "Announcements":
                UIView.animate(withDuration: 0.25, animations: { () -> Void in
                    self.colorTabBar("blue")
                })
                break
            case "Schedule":
                UIView.animate(withDuration: 0.25, animations: { () -> Void in
                    self.colorTabBar("red")
                })
                break
            case "Maps":
                UIView.animate(withDuration: 0.25, animations: { () -> Void in
                    self.colorTabBar("green")
                })
                break
            default:
                break
            }

          }
        }

     func colorTabBar(_ color: String){

        switch (color){
        case "blue":
            self.tabBar.tabBarBackgroundColor = .systemColor("blue")
            self.tabBarItem1.tintColor = .white
            self.tabBarItem2.tintColor = .blue
            self.tabBarItem3.tintColor = .blue
            break;
        case "red":
            self.tabBar.tabBarBackgroundColor = .systemColor("red")
            self.tabBarItem1.tintColor = .systemColor("darkRed")
            self.tabBarItem2.tintColor = .white
            self.tabBarItem3.tintColor = .systemColor("darkRed")
            break;
        case "green":
            self.tabBar.tabBarBackgroundColor = .systemColor("green")
            self.tabBarItem1.tintColor = .systemColor("darkGreen")
            self.tabBarItem2.tintColor = .systemColor("darkGreen")
            self.tabBarItem3.tintColor = .white
            break;
        default:
            break;
        }

    }

}
ghost commented 7 years ago

Hi. I dig your code. It is awesome. But i want to print some values when i click a tab bar item everytime. I can print some value with func tabBarController(_ tabBarController: BATabBarController, didSelect viewController: UIViewController) . But this calls one time for one click. Can you help? Thanks.

MaeseppTarvo commented 7 years ago

Not really smart to initialize all viewcontrollers! Is there a workaround?

antiguab commented 6 years ago

Looks like @youngvz has the right idea. @ferdibayrak and @MaeseppTarvo - if you're still interested in getting your feedback incorporated, open a new issue and I'l gladly take a look!