eggswift / ESTabBarController

:octocat: ESTabBarController is a Swift model for customize UI, badge and adding animation to tabbar items. Support lottie!
MIT License
5.17k stars 578 forks source link

By default first tab not showing as selected if tab bar used as RootViewController #33

Closed ajaybeniwal closed 7 years ago

ajaybeniwal commented 7 years ago

I am using ESTabBarController as root view controller of my app, but the problem is that the first controller is shown by default but the tab bar is not showing as selected by default Once I click the tab then it works fine. I have attached screenshot with the issue

class ECPTabBarController: ESTabBarController {

    override func viewDidLoad() {
        self.isHeroEnabled = true
        self.heroTabBarAnimationType = HeroDefaultAnimationType.push(direction: HeroDefaultAnimationType.Direction.left)
        addChildVC(childVc: DashboardViewController(), title: "Dashboard", iconImage: "Dashboard")
        addChildVC(childVc: AddActionViewController(), title: "Add New", iconImage: "AddNew")
        addChildVC(childVc: PendingActionViewController(), title: "Pending Items", iconImage: "Notification")
        addChildVC(childVc: SettingsViewController(), title: "Settings", iconImage: "Settings")
        super.viewDidLoad()
        self.selectedIndex = 0

![firstimage](https://cloud.githubusercontent.com/assets/658004/24071001/fd825754-0c02-11e7-9edd-98d52751ccb0.jpg)
![secondimage](https://cloud.githubusercontent.com/assets/658004/24071002/01bf0b64-0c03-11e7-915c-cf0ad3a06e5b.jpg)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    fileprivate func addChildVC(childVc:UIViewController , title:String,iconImage:String)->Void{
        childVc.tabBarItem = ESTabBarItem.init(TabbarBounceContentView(), title: "Home", image: UIImage(named: iconImage), selectedImage: UIImage(named: iconImage))
        let navigationController = ECPBaseNavigationController(rootViewController: childVc)
        self.addChildViewController(navigationController);

    }

}
class TabbarBasicContentView: ESTabBarItemContentView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        textColor = UIColor.gray
        highlightTextColor = UIColor.ecpBlueNotationColor()
        iconColor = UIColor.gray
        highlightIconColor = UIColor.ecpBlueNotationColor()    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
class TabbarBounceContentView: TabbarBasicContentView {

    public var duration = 0.3

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func selectAnimation(animated: Bool, completion: (() -> ())?) {
        self.bounceAnimation()
        completion?()
    }

    override func reselectAnimation(animated: Bool, completion: (() -> ())?) {
        self.bounceAnimation()
        completion?()
    }

    func bounceAnimation() {
        let impliesAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        impliesAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0]
        impliesAnimation.duration = duration * 2
        impliesAnimation.calculationMode = kCAAnimationCubic
        imageView.layer.add(impliesAnimation, forKey: nil)
    }
}

Do i need to any other extra configuration to make it work. Screenshot is also attached along with.

eggswift commented 7 years ago
self.addChildViewController(navigationController);

The ESTabBarController is not support this way to set viewControllers now. I suggest you to use TabBarController.viewControllers = [xxx]

And I will try to support this way in next version.

ajaybeniwal commented 7 years ago

Yes its works in this way