Reputation: 1711
I'm not using the storyboard mode to make a table along with creating a button to call it. I have the code to make it swipe but for some reason I cant make the button call the sidebar. I have a sideBartableViewController
that creates the tableview
and the SideBar.swift file to give it the functionality. I think I have to give the sidebar.swift file an extra function to open up the sideBar
when the button is pressed. All I have is the swipe motion embedded into the SideBar.swift file. Any help would be appreciated!If you need the code for the sideBarTableViewController
or SideBar
I can post it up
class ViewController: UIViewController, SideBarDelegate {
var sideBar:SideBar = SideBar()
override func viewDidLoad() {
super.viewDidLoad()
// Menu Button
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(0, 17, 45, 43)
//button.backgroundColor = UIColor.greenColor()
//button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
var buttonMenu = UIImage(named: "menu-button.png")
var buttonMenuView = UIImageView(frame: CGRectMake(0, 17, 45, 43))
buttonMenuView.image = buttonMenu
self.view.addSubview(buttonMenuView)
// Side bar action and text
sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"])
sideBar.delegate = self
}
func buttonAction(sender:UIButton!)
{
if sideBar = SideBar.self{
sideBarWillOpen()
}else{ sideBarWillClose()
}
}
}
Upvotes: 0
Views: 304
Reputation: 2789
I incorrectly assumed your sideBar was the UIViewController. After seeing your SideBar Class, I see it's an NSObject that handles showing/hiding the table view controller. So all you have to do is check if the sideBar is open or not and show/hide it accordingly.
@IBAction func buttonAction(sender: AnyObject) {
if sideBar.isSideBarOpen {
sideBar.showSideBar(false)
} else {
sideBar.showSideBar(true)
}
}
Upvotes: 2
Reputation: 2789
You have to add the sideBar.view to the view hierarchy.
Here's a general way to do a custom menu/sidebar.
func showSideBar() {
// if sideBar is nil, then init it and set delegate
if sideBar == nil {
// init the sideBar
sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"])
sideBar.delegate = self
}
// add it off the right side of the screen
sideBar.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) // customize the width and height here
self.view.addSubview(sideBar.view)
// animate onto the screen
UIView.animateWithDuration(0.4, animations: {()
self.sideBar.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)
})
}
func hideSideBar() {
if sideBar != nil {
// animate off the right side of the screen
UIView.animateWithDuration(0.4, animations: {()
self.sideBar.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)
})
}
}
Upvotes: 0