pmusolino / PMAlertController

PMAlertController is a great and customizable alert that can substitute UIAlertController
MIT License
2.53k stars 186 forks source link

Black background when using Tabbar #88

Closed grosetti closed 5 years ago

grosetti commented 5 years ago

Hello Paolo,

I detected a problem when using PMAlert on a Tabbed Application. When I change the tab with the alert and then returning to the controller the background is black. The problem, however, is only when the view has a navigation controller, otherwise not. The same situation with the UIAlert does not present the problem. How can I fix it?

Thank you in advance Peppe

Simulator Screen Shot - iPhone Xʀ - 2019-06-08 at 14 47 59

Burakpusat commented 5 years ago

I've came across with this before and actually saw a suggestion below -cannot reference since I couldn't find the original post-

guard let rootVC = UIApplication.shared.keyWindow?.rootViewController else {
                return }
            rootVC.present(showAllSeasonVC, animated: true, completion: nil)

This solves the problem but of course Paolo might have better suggestions.

grosetti commented 5 years ago

Thanks for your suggestion, I've tried to use this code but in my architecture it doesn't open the alert because " the vc is not in the window hierarchy". So I don't understand how modify the code.

florianldt commented 5 years ago

Your issue is probably due to the alert being presented twice. modalPresentationStyle = UIModalPresentationStyle.overCurrentContext covers the current context, meaning that if you have one alert displayed, this alert will cover your UIViewController view normally. In the case where you are showing two alerts at the same time, the current context alert is the first alert, which is after dismissed, creating this black background since the only view remaining now is your UITabBarController view, which is, if you didn't set a color to it, is black.

This issue usually occurs because when you use self.present(myAlertController, animated: true, completion: nil), the alert is displayed on top of your view, which is itself displayed on top of the UITabBarController view, whom UITabBarItems remaining active and usable. By that, you can still use the app and trigger other AlertControllers producing that issue.

I have been looking to understand how the native UIAlertController could cover the whole UIWindow when calling present to it without success from now.

As @Burakpusat suggested:

guard let rootVC = UIApplication.shared.keyWindow?.rootViewController else {
                return }
            rootVC.present(showAllSeasonVC, animated: true, completion: nil)

should be enough to fix your issue.

Intrigued to know why you get that error you mentioned.

Another solution would be to present the alert directly on the UITabBarController, which is a UIViewController subclass, which also would fix your issue.

As seen on your screenshot, you don't have a UINavigationController, so the direct parent of your UIViewController is the UITabBarController. You can now grab it using the parent property on your UIViewController and calling parent?.present(hnAlertViewController, animated: true, completion: nil)

In the case you are using a UITabBarController + UINavigationController, using a simple recursive function can bring you to the UITabBarController:

        ...... 
        guard let tabBarController = findTapBar(from: self) else { return }
        tabBarController.present(hnAlertViewController, animated: true, completion: nil)
    }

       func findTapBar(from child: UIViewController?) -> UITabBarController? {
        if child == nil {
            return nil
        }
        if let tabBarController = child?.parent as? UITabBarController {
            return tabBarController
        }
        return findTapBar(from: child?.parent)
    }
}

Hope it solves your issue.

pmusolino commented 5 years ago

I think that @florianldt explained well how to solve the problem. Feel free to reopen the issue if it's not solved for any reason.