【问题标题】:How to show side menu with swrevealviewController when revealview controller does not exist?当revealview控制器不存在时,如何使用swrevealviewController显示侧面菜单?
【发布时间】:2019-03-30 13:51:57
【问题描述】:

我有一个使用 SwRevealViewController 的侧边菜单。我已经使用界面生成器进行了设置,并且正在按照需要的方式工作。这是故事板的设计

跟随应用程序流程,它总是需要通过处理菜单的红色导航视图控制器,黄色是菜单视图,绿色是我需要在推送通知时呈现的一个视图控制器收到,无论应用程序状态如何。 到目前为止,我可以做到这一点,使用以下代码转到绿色视图控制器而不经过红色视图控制器:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    print("did Recive remote")
    if let messageID = userInfo["gcmMessageIDKey"] {
        print("Message ID: \(messageID)")
    }
    print(userInfo)
    completionHandler()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "porterDetail")
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

在该视图显示后没有导航控制器,甚至无法使用此代码创建侧边菜单。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ico_menu"), style: .plain, target: initialViewController, action: #selector(initialViewController.revealToggle(_:)))

但菜单没有显示,也没有任何反应。知道如何在此视图中显示菜单以允许用户外出,或者有什么其他方法可以查看和保持导航。

【问题讨论】:

    标签: ios swift push-notification swrevealviewcontroller


    【解决方案1】:

    我终于解决了如何在收到推送通知时显示来自 appdelagte 的侧边菜单。 所以这个想法是在视图控制器之前实例化一个导航控制器以呈现导航栏。

    let menuVC = storyboard.instantiateViewController(withIdentifier: "menuVC")
            let chatVC = storyboard.instantiateViewController(withIdentifier: "chatNC")
            ((chatVC as! UINavigationController).topViewController as! newChatViewController).id = id
            let mainRWVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
            mainRWVC.setRear(menuVC, animated: true)
            mainRWVC.setFront(chatVC, animated: true)
            self.window?.rootViewController = mainRWVC
            self.window?.makeKeyAndVisible()
    

    然后在显示的导航的 topViewcontroller 中。检查 id 不是 nil 然后呈现下一个视图。然后在呈现的视图中。在确实加载此代码时显示侧边菜单。

     self.navigationItem.leftBarButtonItem = nil
            let revealViewController = self.revealViewController()
            revealViewController?.rearViewRevealWidth = ((UIScreen.main.bounds.width * 74.6875) / 100)
            if revealViewController != nil{
                let button1 = UIBarButtonItem(image: UIImage(named: "ico_menu"), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))) // action:#selector(Class.MethodName) for swift 3
                    self.navigationItem.leftBarButtonItem  = button1
                self.navigationItem.leftBarButtonItem?.tintColor = .white
            }
    

    瞧,侧面菜单出现并按预期工作。

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 2015-12-08
      • 2019-01-25
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2014-12-20
      相关资源
      最近更新 更多