Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set selectedIndex #4

Open
stwie111 opened this issue Feb 13, 2020 · 7 comments
Open

Unable to set selectedIndex #4

stwie111 opened this issue Feb 13, 2020 · 7 comments

Comments

@stwie111
Copy link

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
    }
} 
@Sandysgit
Copy link

I also tried changing in

class SOTabBar: UIView

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

}

@stwie111
Copy link
Author

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

@kaigothe
Copy link

kaigothe commented Sep 6, 2020

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.

@Ahmadalsofi
Copy link
Owner

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 .

@Jayant-georadius
Copy link

Any solution for this??

@MeetBudheliya
Copy link

MeetBudheliya commented Sep 6, 2022

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...

@khushalhoch23
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants