你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift實現側滑菜單SWRevealViewController

Swift實現側滑菜單SWRevealViewController

編輯:IOS開發綜合

SWRevealViewController Github下載地址

導入

我這裡直接新建一個SWRevealViewController包,然後把代碼直接粘貼進入

 

這裡寫圖片描述

 

在橋接文件中導入

#import “SWRevealViewController.h”

使用

編譯一下,接著新建一個MainController父容器,繼承SWRevealViewController

import UIKit

class MainController: SWRevealViewController{



    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

創建左側菜單欄RightMenuController

import UIKit

class RightMenuController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.greenColor()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

在需要打開MainController把初始布局和左側布局加上

        let one = UINavigationController(rootViewController: ViewController())
        let view = MainController(rearViewController: RightMenuController(), frontViewController: one)
        presentViewC(view)//打開頁面的方法

在ViewController中加入手勢

if self.revealViewController() != nil {
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

運行程序,已經可以看到側滑菜單實現了
這裡寫圖片描述
我們也可以增加左側按鈕的點擊事件

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "菜單", style: .Done, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))

也可以更改菜單欄寬度

self.revealViewController().rearViewRevealWidth = 100

這裡寫圖片描述

菜單欄切換布局

在RightMenuController中添加兩個按鈕,並實現方法

import UIKit

class RightMenuController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.greenColor()
        // Do any additional setup after loading the view.

        let b1 = UIButton(frame: CGRect(x: 0, y: 40, width: 100, height: 40))
        b1.setTitle("b1", forState: .Normal)
        b1.tag = 1
        b1.addTarget(self, action: #selector(self.actionBtn(_:)), forControlEvents: .TouchUpInside)
        self.view.addSubview(b1)

        let b2 = UIButton(frame: CGRect(x: 0, y: 100, width: 100, height: 40))
        b2.setTitle("b2", forState: .Normal)
        b2.tag = 2
        b2.addTarget(self, action: #selector(self.actionBtn(_:)), forControlEvents: .TouchUpInside)
        self.view.addSubview(b2)
    }

    func actionBtn(sender:UIButton){
        var ui:UIViewController!
        if sender.tag == 1{
            ui = ViewController()
        }
        if sender.tag == 2{
            ui = WXController()
        }
        let main = UINavigationController(rootViewController: ui)
        self.revealViewController().pushFrontViewController(main, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

在WXCotroller中寫入和ViewController相同的代碼

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "菜單", style: .Done, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
        self.view.backgroundColor = UIColor.whiteColor()
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

運行效果
這裡寫圖片描述
這裡寫圖片描述

SWRevealViewController包中還可以實現右側菜單,兩重菜單,這裡就不說了。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved