Comments (10)
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.
from animated-tab-bar.
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.
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.
I have the same issue, have you figured out a solution?
from animated-tab-bar.
@cdf1982 @aalzanki Thanks guys, that was helpful, I'll test it and I'll inform you
from animated-tab-bar.
The func setSElectedTabToIndex raises null pointer exception
from animated-tab-bar.
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.
@cdf1982 your soultion worked perfectly, thanks man
from animated-tab-bar.
@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.
@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)
- change behavior when tab bar item tapped twice HOT 4
- Setting image and title via nib not storyboard HOT 1
- Animates two items at the same time HOT 1
- Icons become 30x30 if you make setViewControllers second/third time HOT 1
- How to prohibit pictures from rendering HOT 1
- RTL not supported? HOT 1
- Seting animation programmatically does not work HOT 2
- Animation does not work
- Hiding tabbar issue HOT 1
- TabBarItem text color doesn't obey the tint color when selected HOT 1
- Demo is not working HOT 2
- First tabbarItem Text tintColor is wrong HOT 2
- selectedImage property makes me confused HOT 1
- Followed everything but I didn't get anythings HOT 5
- The dividing line disappears HOT 2
- How to integrate with lottie?
- iOS 16 crashes HOT 3
- bar appering over iOS bottom line
- Increase TabBar Height
- Do it support git image
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from animated-tab-bar.