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.
for the UIBarButtonItem this works
@IBOutlet weak var sideButton: UIBarButtonItem!
@IBAction func sideButtonPress(sender: UIBarButtonItem) {
revealViewController().revealToggle(sender)
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With