首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SideBar解密

SideBar解密
EN

Stack Overflow用户
提问于 2014-11-06 20:52:11
回答 2查看 300关注 0票数 0

我不使用故事板模式来创建一个表,同时创建一个按钮来调用它。我有代码使它滑动,但由于某些原因,我不能让按钮调用侧边栏。我有一个sideBartableViewController来创建tableviewSideBar.swift文件来赋予它这个功能。我认为我必须给sidebar.swift文件一个额外的函数,以便在按下按钮时打开sideBar。我所拥有的只是嵌入到SideBar.swift文件中的滑动动作。如果您需要sideBarTableViewControllerSideBar的代码,我可以把它张贴起来。

代码语言:javascript
复制
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()
    }



    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-07 16:08:47

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

代码语言:javascript
复制
@IBAction func buttonAction(sender: AnyObject) {
    if sideBar.isSideBarOpen {
        sideBar.showSideBar(false)
    } else {
        sideBar.showSideBar(true)
    }
}
票数 2
EN

Stack Overflow用户

发布于 2014-11-06 22:12:39

必须将sideBar.view添加到视图层次结构中。

这里有一种通用的方法来做自定义菜单/侧边栏。

代码语言:javascript
复制
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)
        })
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26789159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档