Unable to set selectedIndex
stwie111 opened this issue · 7 comments
i am trying to set initial index to '2' but it's not working.
I even tried to change the selectedIndex in the library but its not working.
func setUpTabBarController(){
self.delegate = self
self.selectedIndex = 2
let homeStoryboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeViewController")
let searchStoryboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchViewController")
let recordStoryboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RecordViewController")
let notificationStoryboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationViewController")
let profileStoryboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "UserProfileViewController")
homeStoryboard.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_Selected"))
searchStoryboard.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "search"), selectedImage: UIImage(named: "search-selected"))
recordStoryboard.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "record"), selectedImage: UIImage(named: "record"))
notificationStoryboard.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "notification"), selectedImage: UIImage(named: "notification-selected"))
profileStoryboard.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "profile"), selectedImage: UIImage(named: "profile-selected"))
viewControllers = [homeStoryboard, searchStoryboard,recordStoryboard,notificationStoryboard,profileStoryboard]
}
i also tried to set the index in the podfile but it's not working for me.
open class SOTabBarController: UIViewController, SOTabBarDelegate {
weak open var delegate: SOTabBarControllerDelegate?
public var selectedIndex: Int = 2
public var previousSelectedIndex = 0
public var viewControllers = [UIViewController]() {
didSet {
tabBar.viewControllers = viewControllers
}
}
I also tried changing in
class SOTabBar: UIView
internal var viewControllers = [UIViewController]() {
didSet {
drawTabs()
guard !viewControllers.isEmpty else { return }
drawConstraint()
layoutIfNeeded()
didSelectTab(index: 2)
}
}
It would be great if there is a function to change the selectedTab from any of the child class.
My solution is, copy the 4 swift files form Pod in your Project and delete the pod. After that you can modify the didSelectTab(index: 1)
in public class SOTabBar: UIView
.
Hi @kaigothe, did you find the solution if so, you can push it to the repo and we are happy to have you with us as a contributor .
Any solution for this??
My solution is, copy the 4 swift files form Pod in your Project and delete the pod. After that you can modify the
didSelectTab(index: 1)
inpublic class SOTabBar: UIView
.
@kaigothe Thanks, it's working...
Hi @kaigothe, did you find the solution if so, you can push it to the repo and we are happy to have you with us as a contributor .
I have solution for above issue, follow the steps.
Line No : 137 , Class SOTabBar
func didSelectTab(index: Int) {
if index + 1 == selectedIndex {return}
animateTitle(index: index)
previousSelectedIndex = selectedIndex
selectedIndex = index + 1
delegate?.tabBar(self, didSelectTabAt: index)
animateCircle(with: circlePath)
animateImage()
guard let image = self.viewControllers[index].tabBarItem.selectedImage else {
fatalError("You should insert selected image to all View Controllers")
}
self.tabSelectedImageView.image = image
}
Step 2: class SOTabBarController, set below function at line no : 77
func preSelectedIndex(index: Int){
self.tabBar.didSelectTab(index: index)
}
Step 3: Call the function from your Tabbar class
class UTabbarVC: SOTabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// your VC setup here
...
// set here you want to selected index
preSelectedIndex(index: 1)
}
}