Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create slide menu using SWReavealController with TabbarViewController programmatically - swift

I am creating a sample app which contains TabBarViewController and also i implement slide menu using SWRevealViewController and the problem is that the slide menu it not showing out.

Here i set rootViewController in appdelegate

 window = UIWindow(frame: UIScreen.main.bounds)

 window?.makeKeyAndVisible()

 window?.rootViewController = TabBarController()

Here is my TabBarController which i implement 2 tabBar items which first tabBar item should have slide menu

    let homeController = HomeController()
    let homeNavigation = UINavigationController(rootViewController: homeController)
    homeNavigation.tabBarItem.title = "Home"
    let menuController = MenuViewController()
    let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
    swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
    swReveal?.toggleAnimationDuration = 0.30

    let favController = FavoriteController()
    let favNavigation = UINavigationController(rootViewController: favController)
    favNavigation.tabBarItem.title = "Favorite"
    viewControllers = [homeNavigation, favNavigation]

For MenuController i setup some navigation button for showing menu

    let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))
    self.navigationItem.leftBarButtonItem = menuButton
@objc func slideMenu() {

    if revealViewController() != nil {
        revealViewController().revealToggle(animated: true)
        revealViewController().rearViewRevealWidth = (view.bounds.width * 80 ) / 100
    }
    else {
        print("no reveal view")
    }
}
like image 342
Visal Sambo Avatar asked Oct 30 '25 15:10

Visal Sambo


1 Answers

You need to configure SWRevealViewController in your didFinishLaunchingWithOptions method as shown below:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let frontNavigationController:UINavigationController
    let rearNavigationController:UINavigationController
    let revealController = SWRevealViewController()
    var mainRevealController = SWRevealViewController()

    frontNavigationController =  UINavigationController(rootViewController: TabBarController())
    rearNavigationController = UINavigationController(rootViewController: MenuViewController())

    frontNavigationController.navigationBar.isHidden = true
    rearNavigationController.navigationBar.isHidden = true

    revealController.frontViewController = frontNavigationController
    revealController.rearViewController = rearNavigationController
    revealController.delegate = self
    mainRevealController  = revealController

    window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = mainRevealController
    self.window?.makeKeyAndVisible()

    return true
}

And in your HomeController replace

let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self.revealViewController(), action: #selector(slideMenu))

with

let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(slideMenu))

And remove

let menuController = MenuViewController()
let swReveal = SWRevealViewController(rearViewController: homeNavigation, frontViewController: menuController)
swReveal?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swReveal?.toggleAnimationDuration = 0.30

From your TabBarController and your final code will be:

func customTabbar (){

    let homeController = HomeController()
    let homeNavigation = UINavigationController(rootViewController: homeController)
    homeNavigation.tabBarItem.title = "Home"

    let favController = FavoriteController()
    let favNavigation = UINavigationController(rootViewController: favController)
    favNavigation.tabBarItem.title = "Favorite"

    let servicePhoneCollectionFLowLayoutInstance = UICollectionViewFlowLayout()
    let serviceTabbarFlowLayoutInit = ExploreController(collectionViewLayout: servicePhoneCollectionFLowLayoutInstance)

    let exploreController = serviceTabbarFlowLayoutInit
    let exploreNavigation = UINavigationController(rootViewController: exploreController)
    exploreNavigation.tabBarItem.title = "Explore"


    let moreController = DetailViewController()
    let moreViewController = UINavigationController(rootViewController: moreController)
    moreViewController.tabBarItem.title = "Tools"
    viewControllers = [homeNavigation, favNavigation, exploreNavigation, moreViewController]
}

And for more info check your updated project HERE.

Here is Working Code of SWRevealViewController with UINavigationController and UITabBarController by Storyboard (Swift 4)

https://stackoverflow.com/a/51725803/10150796

like image 68
Dharmesh Kheni Avatar answered Nov 02 '25 05:11

Dharmesh Kheni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!