SideBar声明

时间:2014-11-06 20:52:11

标签: ios swift sidebar

我没有使用故事板模式制作表格,同时创建一个按钮来调用它。我有代码使它滑动但由于某种原因我不能让按钮调用侧边栏。我有sideBartableViewController创建tableview SideBar.swift 文件,以便为其提供功能。我想我必须给 sidebar.swift 文件一个额外的功能,以便在按下按钮时打开sideBar。我所拥有的是嵌入到 SideBar.swift 文件中的滑动动作。任何帮助将不胜感激!如果您需要sideBarTableViewControllerSideBar的代码我可以发布

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()
    }



    }
}

2 个答案:

答案 0 :(得分:2)

我错误地认为你的sideBar是UIViewController。看到你的SideBar类后,我发现它是一个处理显示/隐藏表视图控制器的NSObject。所以你要做的就是检查sideBar是否打开并相应地显示/隐藏它。

@IBAction func buttonAction(sender: AnyObject) {
    if sideBar.isSideBarOpen {
        sideBar.showSideBar(false)
    } else {
        sideBar.showSideBar(true)
    }
}

答案 1 :(得分:0)

您必须将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)
        })
    }
}