Minitour / AZTabBarController

A custom tab bar controller for iOS written in Swift 4.2
MIT License
348 stars 65 forks source link

Accessing AZTabBar instance #4

Closed ElyDantas closed 7 years ago

ElyDantas commented 7 years ago

How to access AZTabBar from another ViewController class ?

I want to hide tabbar when button is clicked in another viewcontroller storyboard/class

ElyDantas commented 7 years ago

By making tabController static... dah

public static var tabController:AZTabBarController!

Minitour commented 7 years ago

@ElyDantas The AZTabBar doesn't include this kinda of feature out of the box, however I can provide a simple solution for you:

Create a custom protocol and specify the events you wish to trigger:

protocol MyCustomDelegate{
    func requestTabVisability(show: Bool, animate: Bool)
}

Then conform your ViewController in which you have an instance member of AZTabBarController to that Custom Delegate:

extension ViewController: MyCustomDelegate{
    func requestTabVisability(show: Bool, animate: Bool){
             self.tabBar.setBar(hidden: !show, animated: animate)
    }
} 

Then in your child view controller, create an instance member of that protocol:

class MyChildViewController: UIViewController{
       ...
        var delegate: MyCustomDelegate?
       ...
}

Now when you are about to add the child view controller to the tab bar simply do this:

let myChildViewController = MyChildViewController()
myChildViewController.delegate = self //we set the delegate to point to ViewController
tabController.set(viewController: myChildViewController, atIndex: 0)

So now every time you wish to hide the tab bar you can do so by doing:

self.delegate?.requestTabVisability(show: true,animate: true)

I hope this helps you solve your problem, if you have any question feel free to ask 😄

Minitour commented 7 years ago

@ElyDantas Was your issue resolved?

ElyDantas commented 7 years ago

Yes, it worked like a charm !!! Thx, now it is more design pattern driven.

Minitour commented 7 years ago

Glad to help.