Giter Site home page Giter Site logo

Comments (10)

cdf1982 avatar cdf1982 commented on May 4, 2024 22

It looks like I made it work, but I don't really like my own solution, so anyone more expert than me (meaning, everyone ;) is more than welcome to suggest a better way.

If you need to start your app in a different tab from the first one, it's pretty straightforward; in AppDelegate, inside application:DidFinishLaunchingWithOptions:

        let ramTBC = self.window!.rootViewController as! RAMAnimatedTabBarController

        ramTBC.selectedIndex = myDestination

        ramTBC.setSelectIndex(from: 0, to: myDestination)  // Because by default createCustomIcons() of RAMAnimatedTabBarController sets the selectedState for the item at index 0

If you need to switch programmatically from one tab to another during the execution of the app:

        let currentIndex : Int? = self.tabBarController?.selectedIndex

        self.tabBarController?.selectedIndex = myDestination

        if let ramTBC = self.tabBarController as? RAMAnimatedTabBarController,
           let current = currentIndex {

            ramTBC.setSelectIndex(from: current, to: myDestination)
        }

I hope it helps.

@cdf1982

from animated-tab-bar.

aalzanki avatar aalzanki commented on May 4, 2024 2

I just added a new method called setSelectedTabToIndex to the RAMAnimatedTabBarController class

func setSelectedTabToIndex(index: Int) {
    let items = tabBar.items as! [RAMAnimatedTabBarItem]

    let currentIndex = index
    if selectedIndex != currentIndex {
        let animationItem : RAMAnimatedTabBarItem = items[currentIndex]
        let icon = iconsView[currentIndex].icon
        let textLabel = iconsView[currentIndex].textLabel
        animationItem.playAnimation(icon, textLabel: textLabel)

        let deselelectIcon = iconsView[selectedIndex].icon
        let deselelectTextLabel = iconsView[selectedIndex].textLabel
        let deselectItem = items[selectedIndex]
        deselectItem.deselectAnimation(deselelectIcon, textLabel: deselelectTextLabel)

        selectedIndex = index
    }
}

from animated-tab-bar.

wei-go avatar wei-go commented on May 4, 2024 1

Based on @aalzanki solution,
I modified the function as easier below

RAMAnimatedTabBarController.swift

open func setSelectedTabToIndex(index: Int) {
        guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else {
            fatalError("items must inherit RAMAnimatedTabBarItem")
        }
        let currentIndex = index
        if selectedIndex != currentIndex {
            let animationItem : RAMAnimatedTabBarItem = items[currentIndex]
            let animationItemSelected : RAMAnimatedTabBarItem = items[selectedIndex]
            animationItem.playAnimation()
            animationItemSelected.deselectAnimation()
            selectedIndex = index
        }
    }

Usage
If your TabBarController is addressed under app.window as rootViewController,
you can make a class function to control your TabBarController

AppHelper.swift

class AppHelper: NSObject {
class func selectTabBarIndexTo(index: Int) -> Void {
        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            (window.rootViewController as! RAMAnimatedTabBarController).setSelectedTabToIndex(index: index)
        }
    }
}

and call above function from the other ViewController class

AppHelper.selectTabBarIndexTo(index: 0) // To tabIndex

@aalzanki Thanks for your answer to solved my issue 👍

from animated-tab-bar.

cdf1982 avatar cdf1982 commented on May 4, 2024

I have the same issue, have you figured out a solution?

from animated-tab-bar.

TareqElMasriDev avatar TareqElMasriDev commented on May 4, 2024

@cdf1982 @aalzanki Thanks guys, that was helpful, I'll test it and I'll inform you

from animated-tab-bar.

randeepbhatia avatar randeepbhatia commented on May 4, 2024

The func setSElectedTabToIndex raises null pointer exception

from animated-tab-bar.

cdf1982 avatar cdf1982 commented on May 4, 2024

Have you tried my approach?

Il giorno 05/ago/2015, alle ore 02:14, Randeep [email protected] ha scritto:

The func setSElectedTabToIndex raises null pointer exception


Reply to this email directly or view it on GitHub.

from animated-tab-bar.

TareqElMasriDev avatar TareqElMasriDev commented on May 4, 2024

@cdf1982 your soultion worked perfectly, thanks man

from animated-tab-bar.

sentiasa avatar sentiasa commented on May 4, 2024

@cdf1982 Thanks man! I tried the AppDelegate part and your solution worked perfectly.

However, my icon is using BounceAnimation but for the initial part (the AppDelegate part), the icon gets initialised with some offset below.. Is there a way to push it back up manually in the AppDelegate (to achieve it with a workaround) because on the other times later in the app, it behaves as expected. It's just the AppDelegate part. What may be the problem? How can I overcome this do you think? Thanks

from animated-tab-bar.

sesur avatar sesur commented on May 4, 2024

@cdf1982 I have found other way to set up you preferred tab when app starts, use:

func viewWillAppear(_ animated: Bool) 
{
        super.viewWillAppear(true)
         setSelectIndex(from: 0, to: yourPreferredTab)
    }

from animated-tab-bar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.