Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu from tab bar in storyboarding

At the moment I have a standard tab bar, when tapped goes to its corresponding viewController. But I want to make a menu pop out from tab bar when more tab is selected, as shown in below image.

enter image description here

Is any suggestion to implement this? Thanks in advance.

like image 795
Hassan Mahmood Avatar asked Dec 18 '12 00:12

Hassan Mahmood


People also ask

How do I add a tab bar to a storyboard?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is a tab navigation bar?

Tab bars use bar items to navigate between mutually exclusive panes of content in the same view. Tab bars help people understand the different types of information or functionality that a view provides.


1 Answers

I would recommend you do so:

First, you should think of all the tab types that could be in tab bar. On your screenshot there are tabs, that present controller, and tab, that presents menu. So we could create enum with all these types:

enum TabType {
    case controller
    case menu
}

After that you can store array of tab types in order they are shown in tab bar, for your screenshot like so

let tabTypes: [TabType] = [.controller, .controller, .controller, .controller, .menu]

Then you should implement UITabBarControllerDelegate's func tabBarController(_:, shouldSelect:) -> Bool method, which returns true if tab bar is allowed to select the passed controller, and false otherwise.

If you return true than all other work (like presenting view controller and other stuff) tab bar controller will do for you.

In your case you want to execute custom action on tab click, so you should return false. Before returning you should present your menu.

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if let index = tabBarController.viewControllers?.index(of: viewController),
        index < tabBarTypes.count {
        let type = tabBarTypes[index]
        switch type {
        case .menu:
            // perform your menu presenting here
            return false
        case .controller:
            // do nothing, just return true, tab bar will do all work for you
            return true
        }
    }
    return true
}

In this implementation you can easily change tab types order or add some another tab type and handle it appropriate.

like image 122
LowKostKustomz Avatar answered Oct 03 '22 16:10

LowKostKustomz