Using SWRevealViewController in Swift

Posted By Pawanpreet Singh | 31-Dec-2014

SWRevealViewController is a side view controller inspired by facebook menu style written in Objective-C. Here I will show you how can you use this in your swift based application. Please do the following:

 

  1. Download the SWRevealController from this github link.
  2. Make a new single view based application in Xcode, select swift as language.
  3. Drag one more View Controller and a Table View Controller to main storyboard.
  4. Drag SWRevealViewController.m file from the place where you have downloaded the SWRevealController (You can find this file inside SWRevealViewController folder), and Click “Yes” to the prompted message Would you like to configure an Objective-C bridging header?
  5. Drag SWRevealViewController.h in your project from same place.
  6. Select your Root View Controller from storyboard and from Identity Inspector change its class from ViewController to SWRevealViewController.
  7. Right click on Root View Controller and create a segue to Table View Controller select Reveal View set Controller from manual segue. repeat same for other View Controller.
  8. Select segue of Table View Controller and in Attributes Inspector add sw_rear as Identifier.
  9. Select segue of Other View Controller and in Attributes Inspector add sw_front as Identifier.
  10. In the generated briding file add the following code
      #import "SWRevealViewController.h"
            


What we have done till now:-

There are Three Controllers in main storyboard our root controller is of type SWRevealController which will handle our functionality. We created segue to identify SWRevealController which will show in front and which view will be as rear. Front view will be your main screen and rear view will be treated as menu.

  1. Create new cocoa touch class which will be subclass of UITableViewController, name it as MenuTableViewController and a cocoa touch class subclass of UIViewController and name it as MainViewController.
  2. Change the class of TableViewController to MenuTableViewController and other ViewController to MainViewController.
  3. Open MenuTableViewController.swift file and add this below import statement.
    struct Bird{
        var name:String!
    }
            
  4. Change the following methods of your class as shown below.
    override func viewDidLoad() {
            super.viewDidLoad()
            birds.append(Bird(name: "Pigeon"))
            birds.append(Bird(name: "Crow"))
            birds.append(Bird(name: "Peacock"))
            birds.append(Bird(name: "Duck"))
            birds.append(Bird(name: "Parrot"))
            birds.append(Bird(name: "Eagle"))
        }
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return birds.count
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
            cell.textLabel?.text = birds[indexPath.row].name
            // Configure the cell...
    
            return cell
        }
            
  5. Select cell in TableViewController from main storyboard and change its identifier using Attributes inspector to myCell.
  6. Make delegate, datasource of this TableViewController to self by right clicking on table and drag to yellow icon above table view controller and select delegate and datasource one by one.
  7. Add a button to other view controller which is now MainViewController on top left corner of this view and open MainViewController.swift as side by side using Assistant Editor and create an outlet named menuButton in swift file.
  8. Add the follwing code in viewDidLoad of MainViewController class.
    menuButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: UIControlEvents.TouchUpInside) 
            
  9. Run and Test the app.

 

 

Request for Proposal

Recaptcha is required.

Sending message..