Dismissing viewController by pressing on tabbar button remain black screen swift 5

风流意气都作罢 提交于 2020-01-15 09:18:50

问题


I have a custom action sheet viewController. Which is presented modally on the current top view controller. Like this:

//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
    }
}

And I am dismissing it like this:

@objc func dismissViewController() {
        DispatchQueue.main.async {
            if let topViewController = UIApplication.getTopViewController() {
               topViewController.dismiss(animated: true, completion: nil)
            }
            NotificationCenter.default.removeObserver(self)
        }
    }

It's working perfectly fine. I have added the notification observer in my customTabbarController, to dismiss the action sheet if user tap on some another tabbar button like this:

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)
    }

The action sheet is right now presenting on Home tab > Profile (by push) > Action sheet (by modal). So if I tap on Home tab again it will dismiss the action sheet viewController and come back to Home perfectly. But if I tap on some other tabbar button rather than home and come back home, it shows a black screen. What I am missing here? Any suggestions would be highly appreciable.


回答1:


I guess your code calls dismissViewController() twice.

  1. When you press other tab, it removes the action sheet
  2. And then you click home tab and it calls dismissViewController again, and now it removes the homescreenVC


来源:https://stackoverflow.com/questions/58894859/dismissing-viewcontroller-by-pressing-on-tabbar-button-remain-black-screen-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!