5
votes

I am using the 'plugin' SWRevealViewController to help produce a sidebar in my app. Now in objective-C, you can control the side bar using the following code:

[self.sidebarButton setTarget: self.revealViewController];
        [self.sidebarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

Where sidebarButton has been hooked up UIBarButtonItem and IBOutlet. Now I am trying to apply this to my Swift code and I have mixed success.

I've set an @IBOutlet var button. Now I've tried both UIBarButtonItem and UIButton. When I try UIBarButtonItem, I have used the following line:

button = UIBarButtonItem(barButtonSystemItem: .Add, target: self.revealViewController(), action: "revealToggle:")

However, this does nothing and the button doesn't function. However, if I set button to be a UIButton and hook this up with the following:

button.addTarget(self.revealViewController(), action:"revealToggle:", forControlEvents:UIControlEvents.TouchUpInside)

This works to slide the bar out and in but not via gestures. The issue is that I can't add a UIButton to the toolbar (in StoryBoard anyway).

So is there any way to implement this properly? Additionally, is it possible to add the gesture recogniser as well?

Thanks

EDIT

Okay I've managed to get the gesture toggle working, although I think my method is a bit long winded:

var swipeRight = UISwipeGestureRecognizer(target: self.revealViewController(), action: "revealToggle:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeLeft = UISwipeGestureRecognizer(target: self.revealViewController(), action: "revealToggle:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

So please correct.

4
Hello. I have the same problem with gesture. I have tried your solution but it's look not good. Did you found another way to do it? Thanks.Slavik Voloshyn
I found a better solution for attach gesture. Put it to your front ViewController. self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());Slavik Voloshyn
could you add your swift code as an answer?Shlomi Schwartz
How u moved from reveal view controller to front view controller in swift ? I have added SWrevealViewController classes in my swift project but I have problem while moving from rear to front.In tableview didselect method ?poojathorat

4 Answers

6
votes

for the UIBarButtonItem this works

@IBOutlet weak var sideButton: UIBarButtonItem!

@IBAction func sideButtonPress(sender: UIBarButtonItem) {
       revealViewController().revealToggle(sender)
}
2
votes

Thanks to Slavik Voloshyn, even though I tried this earlier and it didn't work, the best method is:

self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());

1
votes
@IBOutlet weak var menu: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    if self.revealViewController() != nil {
        menu.target = self.revealViewController()
        menu.action = "revealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}
0
votes

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. We follow this process in Oodles Technologies.

struct Bird{
    var name:String!
}

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
    }

Select cell in TableViewController from main storyboard and change its identifier using Attributes inspector to myCell.

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.

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.

Add the follwing code in viewDidLoad of MainViewController class.

menuButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: UIControlEvents.TouchUpInside)

Run and Test the app.