通过按下标签栏按钮关闭viewController,保持黑屏快速5

问题描述 投票:0回答:1

我有一个自定义的动作表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并完美返回到“主页”。但是,如果我点击其他一些标签栏按钮而不是首页,然后返回首页,它将显示黑屏。我在这里想念的是什么?任何建议都是非常可取的。

enter image description here

ios swift uitabbar modalviewcontroller presentmodalviewcontroller
1个回答
0
投票

我可以建议另一种方法,使用此行,无论您身在何处,您都必须能够进入根视图控制器:

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)

© www.soinside.com 2019 - 2024. All rights reserved.