【问题标题】:SWRevealViewController toggle not working with custom Navigation Bar ButtonSWRevealViewController 切换不适用于自定义导航栏按钮
【发布时间】:2016-07-09 16:20:03
【问题描述】:

我正在使用 SWRevealViewController 在 Swift 2.0 中显示侧边菜单我已经从 GitHub 下载了文件,创建了桥接头并正确连接了 segue。我正在创建一个自定义按钮,但“显示切换”操作没有触发。我在哪里做错了?

super.viewDidLoad()
    self.navigationController?.navigationBar.topItem?.title = "DASHBOARD"
    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController!.navigationBar.titleTextAttributes =
        [NSForegroundColorAttributeName: UIColor.whiteColor()]
    self.navigationController!.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Teko-Regular", size: 26)!]
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    let btnName = UIButton()
    btnName.setImage(UIImage(named: "LeftNavItem"), forState: .Normal)
    btnName.frame = CGRectMake(0, 0, 20, 20)

    let leftBarButton = UIBarButtonItem()
    leftBarButton.customView = btnName
    self.navigationItem.leftBarButtonItem = leftBarButton
    if self.revealViewController() != nil {
        //print(self.revealViewController())
        leftBarButton.target = self.revealViewController()
        leftBarButton.action = Selector("revealToggle:")

        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

手势识别器正常触发,但“revealToggle:”不正确,这是什么错误?

【问题讨论】:

    标签: ios swift swrevealviewcontroller


    【解决方案1】:

    我在使用 SWRevealViewController 时遇到了同样的问题。 解决方案:**登录后我之前调用了仪表板屏幕但是现在 我用下面的代码替换了 SWrevealviewcontroller。现在可以正常工作了

     let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let viewController1 =
                        storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
                    self.present(viewController1, animated: true,completion: nil)
    

    *在 viewdidload() 方法中添加以下代码

     if revealViewController() != nil {
            menuoutlet.target = self.revealViewController()
             menuoutlet.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
            self.revealViewController().rearViewRevealWidth = 240
        }
    

    菜单按钮操作代码

      @IBAction func menu(_ sender: Any) {
        let btntitle = (sender as AnyObject).currentTitle!
        self.revealViewController().rearViewController.performSegue(withIdentifier: btntitle!, sender: self.revealViewController().rearViewController)
    }
    

    【讨论】:

    • 这并不能真正回答问题。如果您有其他问题,可以点击 提问。您也可以add a bounty 引起更多关注这个问题。 - From Review
    • 我得到了解决方案@SRack
    【解决方案2】:
    class Name: SWRevealViewControllerDelegate
    
    override func viewDidLoad() {
      super.viewDidLoad()
    
       self.revealViewController().delegate = self
    
    }
    

    【讨论】:

    • 这是正确的答案。只是稍微注意一下“UIBarButton”没有成员“.addTarget”您必须在“UIButton”上执行此操作,然后将该按钮作为子视图添加到“UIBarButton”
    猜你喜欢
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2015-04-09
    • 2019-04-18
    • 1970-01-01
    • 2016-05-26
    • 2020-09-03
    相关资源
    最近更新 更多