我有一个自定义的动作表viewController。在当前的顶视图控制器上模态显示。像这样:
//MARK: Static Func
static func initViewController() -> CustomActionSheetViewController {
let customActionSheetViewController = CustomActionSheetViewController(nibName: "CustomActionSheetViewController", bundle: nil)
return customActionSheetViewController
}
func presentViewController<T: UIViewController>(viewController: T) {
DispatchQueue.main.async {
if let topViewController = UIApplication.getTopViewController() {
viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = .overCurrentContext
topViewController.present(viewController, animated: true, completion: nil)
}
}
}
// MARK: UIApplication extensions
extension UIApplication {
class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)
} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)
} else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}
}
我正在这样解雇它:
@objc func dismissViewController() {
DispatchQueue.main.async {
if let topViewController = UIApplication.getTopViewController() {
topViewController.dismiss(animated: true, completion: nil)
}
NotificationCenter.default.removeObserver(self)
}
}
工作正常。我在customTabbarController
中添加了通知观察器,以在用户点击其他tabbar
按钮时关闭操作表,如下所示:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// print("Selected view controller", viewController)
// print("index", tabBarController.selectedIndex )
let tabbarNotiKey = Notification.Name(rawValue: "TabbarNotiKey")
NotificationCenter.default.post(name: tabbarNotiKey, object: nil, userInfo: nil)
}
操作单现在显示在Home tab > Profile (by push) > Action sheet (by modal)
上。因此,如果我再次点击“主页”选项卡,它将关闭动作表viewController并完美返回到“主页”。但是,如果我点击其他一些标签栏按钮而不是首页,然后返回首页,它将显示黑屏。我在这里想念的是什么?任何建议都是非常可取的。
我可以建议另一种方法,使用此行,无论您身在何处,您都必须能够进入根视图控制器:
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)