UINavigation controller to UITabbarcontroller issue with SWRevealViewController in iOS Swift

℡╲_俬逩灬. 提交于 2019-11-27 22:35:15

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

Your Storyborad it will be like this you not directly assign UITabBarController to revealViewController().frontViewController need to Use Like this.

Code of MenuViewController Like this

import UIKit

class menuViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tblTableView: UITableView!
@IBOutlet weak var imgProfile: UIImageView!

var ManuNameArray:Array = [String]()
var iconArray:Array = [UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()
    ManuNameArray = ["Home","Booking","Profile","Logout"]
    iconArray = [UIImage(named:"home")!,UIImage(named:"message")!,UIImage(named:"map")!,UIImage(named:"setting")!]

    imgProfile.layer.borderWidth = 2
    imgProfile.layer.borderColor = UIColor.green.cgColor
    imgProfile.layer.cornerRadius = 50

    imgProfile.layer.masksToBounds = false
    imgProfile.clipsToBounds = true 
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ManuNameArray.count

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath) as! MenuCell

    cell.lblMenuname.text! = ManuNameArray[indexPath.row]
    cell.imgIcon.image = iconArray[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let revealviewcontroller:SWRevealViewController = self.revealViewController()

    let cell:MenuCell = tableView.cellForRow(at: indexPath) as! MenuCell
    print(cell.lblMenuname.text!)

    if cell.lblMenuname.text! == "Home"
    {
        print("Home Tapped")

        let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        // Here TabbarController is StoryboardID of UITabBarController
        if let tabBarController = mainstoryboard.instantiateViewController(withIdentifier: "TabbarController") as? UITabBarController{

            tabBarController.selectedIndex = 0
            revealviewcontroller.pushFrontViewController(tabBarController, animated: true)
        }
    }
    if cell.lblMenuname.text! == "Booking"
    {
        print("message Tapped")

        let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewcontroller = mainstoryboard.instantiateViewController(withIdentifier: "BookingViewController") as! BookingViewController
        let newFrontController = UINavigationController.init(rootViewController: newViewcontroller)

        revealviewcontroller.pushFrontViewController(newFrontController, animated: true)
    }
    if cell.lblMenuname.text! == "Profile"
    {
        let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        if let tabBarController = mainstoryboard.instantiateViewController(withIdentifier: "TabbarController") as? UITabBarController{

            tabBarController.selectedIndex = 2
            revealviewcontroller.pushFrontViewController(tabBarController, animated: true)
        }
    }
    if cell.lblMenuname.text! == "Logout"
    {
       print("Logout Tapped")
    }
}
}

Code for Other Remaining ViewControllers Same as Following for All Home, Notification, Profile and Booking

import UIKit

class ProfileViewController: UIViewController {

@IBOutlet weak var btnMenuButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    if revealViewController() != nil {

        btnMenuButton.target = revealViewController()
        btnMenuButton.action = #selector(SWRevealViewController.revealToggle(_:))

        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
    }

  }
}

Your Output :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!