1
votes

Because I want to redesign the tab bar UI, I wrote a custom tab bar controller according to https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar

In TabBarViewController's viewDidLoad(), define several subviews corresponding to each tab bar

homeViewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
...
viewControllers = [homeViewController, searchViewController, accountViewController, trendingViewController]

and the main method when tapping tab

@IBAction func didPressTab(_ sender: UIButton) {

        let previousIndex = selectedIndex
        selectedIndex = sender.tag

        tabButtons[previousIndex!].isSelected = false
        let previousVC = viewControllers[previousIndex!]

        // remove previous VC
        previousVC.willMove(toParentViewController: nil)
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParentViewController()

        // set current VC
        sender.isSelected = true
        let vc = viewControllers[selectedIndex]

        addChildViewController(vc)

        // Adjust the size to match content view
        vc.view.frame = contentView.bounds
        contentView.addSubview(vc.view)
        vc.didMove(toParentViewController: self)
    }

I could set a default tab bar index selectedIndex when the tab bar view is loaded. However, how can I switch to next tab bar in homeViewController (without tapping tab bar buttons)?

This doesn't work in homeViewController TabBarViewController().tabButtons[2].isSelected = true TabBarViewController().didPressTab(TabBarViewController().tabButtons[2])

I'm not sure how to get the running tab controller, set the selectedIndex, and update subview in the subview controllers.

2
How about writing this inside homeViewController: TabBarViewController (parentViewController).selectedIndex = 2, or TabBarViewController (parentViewController).didPressTab(TabBarViewController(parentViewController).tabButtons[2])?ppalancica
Do I need to modify parentViewController? error "unresolved identifier" appeared. btw i use swift 3.0user2174595
Sorry, just use "parent".ppalancica
IDE told me to add label coder, TabBarViewController(coder: parent).selectedIndex = 2 And error Cannot convert value of type 'UIViewController?' to expected argument type 'NSCoder' Did I miss something?user2174595
Looks like parent is an optional, so you have to unwrap it, use parent! instead of just parent.ppalancica

2 Answers

0
votes

All you need to do is to call tabBar.setSelectedViewController: and pass the view controller.

If you only know the tab index, you call tabBar.viewControllers[index] and get the view controller.

0
votes

I finally use Delegate to solve.

In SubViewController add protocol

protocol SubViewControllerDelegate {
    func transferToView(index: Int)
}

and declare this in the class

var delegate: SubViewControllerDelegate?

In TabBarViewController set to conform SubViewControllerDelegate

Implement the method

func transferToView(index: Int) {
    tabButtons[index].isSelected = true
    didPressTab(tabButtons[index])
}

Set delegate

subViewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
let subVC = subViewController as! SubViewController
subVC.delegate = self