当 revealview 控制器不存在时,如何使用 swrevealviewController 显示侧边菜单?
How to show side menu with swrevealviewController when revealview controller does not exist?
我有一个使用 SwRevealViewController 的侧边菜单。我已经使用界面生成器进行了设置,并且正在按照需要的方式工作。这是storyBoard的设计
按照应用流程,它总是需要经过处理菜单的红色导航viewController,黄色的是菜单视图,绿色的是我需要的一个视图控制器当收到推送通知时出现,无论应用程序状态如何。
到目前为止,我可以做到这一点,使用以下代码转到绿色视图控制器而不经过红色视图控制器:
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(_:)))
但是菜单没有显示,也没有任何反应。知道如何在此视图中显示菜单以允许用户外出,或者有什么其他方法可以查看和保持导航。
我终于解决了如何在收到推送通知时显示来自 appdelagte 的侧边菜单。
所以这个想法是在 viewcontroller 之前实例化一个导航控制器来呈现导航栏。
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
}
瞧,侧面菜单出现并按预期工作。
我有一个使用 SwRevealViewController 的侧边菜单。我已经使用界面生成器进行了设置,并且正在按照需要的方式工作。这是storyBoard的设计
按照应用流程,它总是需要经过处理菜单的红色导航viewController,黄色的是菜单视图,绿色的是我需要的一个视图控制器当收到推送通知时出现,无论应用程序状态如何。 到目前为止,我可以做到这一点,使用以下代码转到绿色视图控制器而不经过红色视图控制器:
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(_:)))
但是菜单没有显示,也没有任何反应。知道如何在此视图中显示菜单以允许用户外出,或者有什么其他方法可以查看和保持导航。
我终于解决了如何在收到推送通知时显示来自 appdelagte 的侧边菜单。 所以这个想法是在 viewcontroller 之前实例化一个导航控制器来呈现导航栏。
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
}
瞧,侧面菜单出现并按预期工作。