我不使用故事板模式来创建一个表,同时创建一个按钮来调用它。我有代码使它滑动,但由于某些原因,我不能让按钮调用侧边栏。我有一个sideBartableViewController来创建tableview和SideBar.swift文件来赋予它这个功能。我认为我必须给sidebar.swift文件一个额外的函数,以便在按下按钮时打开sideBar。我所拥有的只是嵌入到SideBar.swift文件中的滑动动作。如果您需要sideBarTableViewController或SideBar的代码,我可以把它张贴起来。
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()
}
}
}发布于 2014-11-07 16:08:47
我错误地认为你的sideBar是UIViewController。在看到您的SideBar类之后,我看到它是一个处理显示/隐藏表视图控制器的NSObject。因此,您所要做的就是检查sideBar是否打开,并相应地显示/隐藏它。
@IBAction func buttonAction(sender: AnyObject) {
if sideBar.isSideBarOpen {
sideBar.showSideBar(false)
} else {
sideBar.showSideBar(true)
}
}发布于 2014-11-06 22:12:39
必须将sideBar.view添加到视图层次结构中。
这里有一种通用的方法来做自定义菜单/侧边栏。
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)
})
}
}https://stackoverflow.com/questions/26789159
复制相似问题