SOTabBar icon indicating copy to clipboard operation
SOTabBar copied to clipboard

Unable to set selectedIndex

Open stwie111 opened this issue 4 years ago • 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
    }
} 

stwie111 avatar Feb 13 '20 07:02 stwie111

I also tried changing in

class SOTabBar: UIView

    internal var viewControllers = [UIViewController]() {
    didSet {
        drawTabs()
        guard !viewControllers.isEmpty else { return }
        drawConstraint()
        layoutIfNeeded()
        didSelectTab(index: 2)
    }

}

Sandysgit avatar Feb 13 '20 12:02 Sandysgit

It would be great if there is a function to change the selectedTab from any of the child class.

stwie111 avatar Feb 14 '20 05:02 stwie111

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.

kaigothe avatar Sep 06 '20 15:09 kaigothe

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 .

Ahmadalsofi avatar Sep 07 '20 16:09 Ahmadalsofi

Any solution for this??

Jayant-georadius avatar Sep 30 '20 09:09 Jayant-georadius

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.

@kaigothe Thanks, it's working...

MeetBudheliya avatar Sep 06 '22 05:09 MeetBudheliya

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)
      }
}

khushalhoch23 avatar Apr 08 '24 10:04 khushalhoch23