没有故事板的Swift SWRevealController

时间:2016-01-13 12:58:24

标签: ios swift swrevealviewcontroller

我用故事板实现了SWRevealController,一切都很好,但现在我想在没有故事板的情况下编写应用程序。

有人可以帮我实施吗?我有一个FrontViewController和一个MenuViewController。我是否需要一个额外的SWRevealViewController,如果是的话,我需要编写什么代码?

我知道SWRevealController上有一些信息,但它在Objective-C中,我在Swift 2.0中写这个有一些问题。

3 个答案:

答案 0 :(得分:5)

您可以按照以下方式执行此操作。

在appDelegate中创建一个函数createSlidingMenu

func createSlidingMenu(){

        let frontViewController = //create instance of frontVC
        let rearViewController  = //create instance of rearVC(menuVC)


        //create instance of swRevealVC based on front and rear VC
        let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController);
        swRevealVC.toggleAnimationType = SWRevealToggleAnimationType.EaseOut;
        swRevealVC.toggleAnimationDuration = 0.30;

        //set swRevealVC as rootVC of windows
        self.window?.rootViewController = swRevealVC!;
}

createSlidingMenu中致电application:didFinishLaunchingWithOptions:,您就可以了。

答案 1 :(得分:0)

这对我有用(在快速3.1中)

  1. 在StoryBoard中创建MasterViewController,HomeViewController(嵌入在UINavigationController中)与storyboardId&#34; HomeViewController&#34;和MenuViewController(嵌入在UINavigationController中)与storyboardId&#34; MenuViewController&#34;。enter image description here < / LI>
  2. 在Storyboard中创建MasterViewController类并关联到UIViewController(MasterViewController)并实现SWRevealViewControllerDelegate协议(参见代码)

    import UIKit
    
    class MasterViewController: UIViewController, SWRevealViewControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let frontNavigationController:UINavigationController
        let rearNavigationController:UINavigationController
        let revealController = SWRevealViewController()
        var mainRevealController = SWRevealViewController()
    
        let menuTable = self.storyboard?.instantiateViewController(withIdentifier:  "MenuViewController")as! MenuViewController
        let homepage = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    
        frontNavigationController =  UINavigationController(rootViewController: homepage)
        rearNavigationController =  UINavigationController(rootViewController: menuTable)
        rearNavigationController.setNavigationBarHidden(true, animated: false)
    
        revealController.frontViewController = frontNavigationController
        revealController.rearViewController = rearNavigationController
        revealController.delegate = self
        mainRevealController  = revealController
    
        UIApplication.shared.delegate!.window?.rootViewController = mainRevealController
    }
    

    }

  3. 作为替代,您可以省略MasterViewController并:

    • 在AppDelegate和
    • 中实现SWRevealViewControllerDelegate
    • 复制上面在应用程序中写的viewDidLoad body的代码:didFinishLaunchingWithOptions:

答案 2 :(得分:0)

除了Hitendra之外,我还修改了以下代码,以便我可以在Home中进行导航

AppDelegate.swift

func toHome() {
    let frontViewController = HomeController()//create instance of frontVC
    let rearViewController  = MenuController() //create instance of rearVC(menuVC)

    let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController);
    swRevealVC?.toggleAnimationType = SWRevealToggleAnimationType.easeOut;
    swRevealVC?.toggleAnimationDuration = 0.30;

    navigationController = UINavigationController(rootViewController: swRevealVC!)
    self.window?.rootViewController = navigationController
}

LoginController.swift,当用户点击登录按钮

@objc private func login() {
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     appDelegate.toHome()
     return
}

HomeController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    setupLayout() // layout programmatically, I didn't put code here

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

}

@objc private func menu() {
    revealViewController().revealToggle(self)
}