在切换屏幕时使用 SideMenu Pod 进行导航会导致错误
Using SideMenu Pod for navigation causes bugs when changing screens
I am using this pod: https://github.com/jonkykong/SideMenu 用于我的应用程序中的侧边栏。我通过 Storyboard 设置侧边栏导航。我的应用程序设计是我有一个主屏幕和一个带有按钮的侧边栏,可以将我带到不同的屏幕。不同的屏幕也有相同的侧边栏。我的错误是在这种情况下:我从主屏幕进入侧边栏 -> 然后我通过侧边栏进入屏幕 2 -> 我从屏幕 2 单击侧边栏 -> 侧边栏从底部出现并获取整个屏幕。
为了修复这个错误,我找到了一个不让我满意的解决方法。我知道我需要关闭侧边栏,但是如果我想进入另一个屏幕,我首先会看到我来自的屏幕(因为我关闭了),然后它只会移动到第二个屏幕。我希望这是有道理的。我想实现不先看主屏就切换到副屏。这是帮助我关闭然后更改屏幕的代码:
dismiss(animated: true, completion: {
goIncident()
})
func goIncident(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window = appDelegate.window
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = mainStoryboard.instantiateViewController(withIdentifier: "log") as! UINavigationController
window?.rootViewController = rootController
}
编辑:添加的照片显示了当我只使用功能 goIncident()
而不关闭时的问题。如果我关闭,我会看到主屏幕,并且只有在主屏幕出现后才会调用新屏幕出现。
所以我用了一个非常丑陋的方法来解决这个问题,但我相信这是唯一可能的方法,因为这个 Pod 不是很灵活,除非我遗漏了什么。我所做的是:
在SideBarViewController
中:
func goLog(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeLog"), object: self)
}
在MainViewController
中:
override func viewDidLoad(){
NotificationCenter.default.addObserver(self, selector: #selector(goLog), name: NSNotification.Name(rawValue: "changeLog"), object: nil)
}
func goLog(){
let logViewController = self.storyboard?.instantiateViewController(withIdentifier: "IncidentLogViewController") as! IncidentLogViewController
self.navigationController?.viewControllers = [logViewController]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissSideBar"), object: self)
}
所以侧边栏告诉导航控制器替换它的视图控制器,然后在它替换后导航控制器发回通知以关闭侧边栏。很丑,但实际上运行起来很流畅。
dismiss(animated: true, completion: {
goIncident()
})
func goIncident(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window = appDelegate.window
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = mainStoryboard.instantiateViewController(withIdentifier: "log") as! UINavigationController
window?.rootViewController = rootController
}
编辑:添加的照片显示了当我只使用功能 goIncident()
而不关闭时的问题。如果我关闭,我会看到主屏幕,并且只有在主屏幕出现后才会调用新屏幕出现。
所以我用了一个非常丑陋的方法来解决这个问题,但我相信这是唯一可能的方法,因为这个 Pod 不是很灵活,除非我遗漏了什么。我所做的是:
在SideBarViewController
中:
func goLog(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeLog"), object: self)
}
在MainViewController
中:
override func viewDidLoad(){
NotificationCenter.default.addObserver(self, selector: #selector(goLog), name: NSNotification.Name(rawValue: "changeLog"), object: nil)
}
func goLog(){
let logViewController = self.storyboard?.instantiateViewController(withIdentifier: "IncidentLogViewController") as! IncidentLogViewController
self.navigationController?.viewControllers = [logViewController]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissSideBar"), object: self)
}
所以侧边栏告诉导航控制器替换它的视图控制器,然后在它替换后导航控制器发回通知以关闭侧边栏。很丑,但实际上运行起来很流畅。