NativeScript / nativescript-datetimepicker

Plugin with date and time picking fields
Apache License 2.0
27 stars 26 forks source link

(iOS) Cannot present datetimepicker alert inside of BottomNavigation tab #81

Open alpha-nero1 opened 4 years ago

alpha-nero1 commented 4 years ago

Make sure to check the demo app(s) for sample usage

Make sure to check the existing issues in this repository

If the demo apps cannot help and there is no issue for your problem, tell us about it

Please, ensure your title is less than 63 characters long and starts with a capital letter.

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Describe the steps to reproduce it.

HI, I have noticed that I can use the date time picker field outside of the BottomNavigation but from within the BottomNavigation I am receiving this warning:

Warning: Attempt to present <UIAlertView: 0x13c9f2e00> on <UINavigationControllerImpl: 0x13c085000> whose view is not in the window hierarchy!

And thus the modal for picking date does not show (This is preventing users of our app from editing their DOB after sign up)

Is there any code involved?

I have meddled with the plugin code and I can observe that somehow (I suspect because of how the bottom navigation is built) the UINavigationControllerImpl that the BottomNavigation makes is not actually a part of the window hierarchy and thus when the plugin tries to show the modal, the modal does not show.

I have also observed that other plugins like nativescript-camera have the same problem and I also cannot use it in the main part of the app (BottomNavigation).

So this leads me to the code. Insise datetimepicker.ios.js in the _showNativeDialog I can see:

DateTimePicker._showNativeDialog = function (nativeDialog, nativePicker, style) {
        var currentPage = datetimepicker_common_1.getCurrentPage();
        if (currentPage) {
            var view = currentPage;
            var viewController = currentPage.ios;
            if (currentPage.modal) {
                view = currentPage.modal;
                if (view.ios instanceof UIViewController) {
                    viewController = view.ios;
                }
                else {
                    var parentWithController = view_1.ios.getParentWithViewController(view);
                    viewController = parentWithController ? parentWithController.viewController : undefined;
                }
            }
            if (viewController) {
                if (nativeDialog.popoverPresentationController) {
                    nativeDialog.popoverPresentationController.sourceView = viewController.view;
                    nativeDialog.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0);
                    nativeDialog.popoverPresentationController.permittedArrowDirections = 0;
                }
                viewController.presentViewControllerAnimatedCompletion(nativeDialog, true, function () {
                });
            }
        }
    };

However, I believe that changing the function to something along the lines of this could do the trick:

DateTimePicker._showNativeDialog = function (nativeDialog, nativePicker, style) {
        var currentPage = datetimepicker_common_1.getCurrentPage();
        if (currentPage) {
            var view = currentPage;
            var rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
            if (rootViewController) {
                if (nativeDialog.popoverPresentationController) {
                    nativeDialog.popoverPresentationController.sourceView = rootViewController.view;
                    nativeDialog.popoverPresentationController.sourceRect = CGRectMake(rootViewController.view.bounds.size.width / 2.0, rootViewController.view.bounds.size.height / 2.0, 1.0, 1.0);
                    nativeDialog.popoverPresentationController.permittedArrowDirections = 0;
                }
                rootViewController.presentViewControllerAnimatedCompletion(nativeDialog, true, function () {
                });
            }
        }
    };

This way the root view controller is alwasy in the window hierarchy and we can rely on it to show every time.

I would be delighted to contribute and launch a PR (after some good testing), but from what I can see I don't have access so I may just fork for the time being.

Please let me know what you think. Thanks and Kind regards!