Swift implements a slide-out menu Swrevealviewcontroller

Source: Internet
Author: User

Swrevealviewcontroller Github

Import

I'm just going to create a new Swrevealviewcontroller package here and paste the code directly into

Import in a bridge file

#import "SWRevealViewController.h"

Use

Compile, then create a new Maincontroller parent container, inherit Swrevealviewcontroller

import uikitclass  MainController: swrevealviewcontroller{override  func viewdidload () {super.viewdidload ()} override  func didreceivememorywarning () {super.didreceivememorywarning () //Dispose of any of the resources that can be recreated.  }/* //MARK:-Navigation  //in a storyboard-based Application, you'll often want to do a little preparation before navigation  overrid E  func prepareforsegue (Segue:uistoryboardsegue, sender:anyobject?) {//Get The new view controller using Segue.destinationviewcontroller.  //Pass the selected object to the new view controller.  } */}

Create left menu bar Rightmenucontroller

Import Uikitclass Rightmenucontroller:uiviewcontroller {override func Viewdidload () {super.viewdidload () Self.view.backgroundColor = Uicolor.greencolor ()// Do  anyAdditional setup AfterLoading theView. } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of  anyResources that can be recreated. }/*//MARK:-Navigation//inchA storyboard-based application, you'll often want to  DoA little preparationbeforeNavigation override func Prepareforsegue (Segue:uistoryboardsegue, sender:anyobject?) {        //GetThe newViewControllerusingSegue.destinationviewcontroller.//Pass The selected object toThe newViewController. }    */}

When you need to open maincontroller the initial layout and left layout are added

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

Adding gestures to Viewcontroller

ifselfnil {    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())}

Running the program, you can already see the slide-out menu implemented

We can also add click events to the left button

self.navigationItem.leftBarButtonItem"菜单".Done, target: self.revealViewController#selector(SWRevealViewController.revealToggle(_:)))

You can also change the menu bar width

self.revealViewController().rearViewRevealWidth100

Menu bar Toggle Layout

Add two buttons to the Rightmenucontroller and implement the method

Import Uikitclass Rightmenucontroller:Uiviewcontroller{override Func Viewdidload () {Super. Viewdidload() Self. View. BackgroundColor=Uicolor. Greencolor()additional setup after loading the view.Let B1 =UIButton(Frame:CGRect(x:0Y: +, Width: -, Height: +)) B1. Settitle("B1", Forstate:. Normal) B1. Tag=1B1. AddTarget( Self, Action:#selector (Self.actionbtn (_:)), forControlEvents:. Touchupinside)         Self. View. Addsubview(B1) Let b2 =UIButton(Frame:CGRect(x:0Y: -, Width: -, Height: +)) B2. Settitle("B2", Forstate:. Normal) B2. Tag=2B2. AddTarget( Self, Action:#selector (Self.actionbtn (_:)), forControlEvents:. Touchupinside)         Self. View. Addsubview(B2)} Func actionbtn (Sender:UIButton) {var UI:Uiviewcontroller!ifSender. Tag==1{UI = Viewcontroller ()}ifSender. Tag==2{UI = Wxcontroller ()} Let main =Uinavigationcontroller(Rootviewcontroller:ui) Self. Revealviewcontroller(). Pushfrontviewcontroller(Main, animated:true)} override func didreceivememorywarning () {Super. didreceivememorywarning()//Dispose of any resources, can be recreated.}/ *//MARK:-Navigation//In a storyboard-based application, you'll often want to does a little preparation be Fore 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. }    */}

Write and Viewcontroller the same code in Wxcotroller

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

Run effect

Swrevealviewcontroller package can also implement the right menu, double menu, here is not said.

Swift implements a slide-out menu Swrevealviewcontroller

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.