alexsorokoletov / Xamarin.iOS.DatePickerDialog

Xamarin iOS C# port of https://github.com/squimer/DatePickerDialog-iOS-Swift
MIT License
24 stars 15 forks source link

Incorrect getting of the current window #16

Closed dimonovdd closed 3 years ago

dimonovdd commented 3 years ago

At the moment, this solution is used (Permalink):

var window = UIApplication.SharedApplication.Windows.Last();

window.AddSubview(this);
window.BringSubviewToFront(this);
window.EndEditing(true);

The most correct approach is to use GetCurrentUIViewController method from Xamarin.Essentials library (Source Code).

var view = Xamarin.Essentials.Platform.GetCurrentUIViewController().View;

view.AddSubview(this);
view.BringSubviewToFront(this);
view.EndEditing(true);
dimonovdd commented 3 years ago

We can also add getCurrentView parameter. This will add flexibility and the project will not have unnecessary dependencies.

public void Show(string title, string doneButtonTitle, string cancelButtonTitle,
    UIDatePickerMode datePickerMode,
    Action<DateTime> callback,
    DateTime defaultDate, DateTime? maximumDate = null,
    DateTime? minimumDate = null,
    Action? cancelCallback = null,
    Func<UIView> getCurrentView = null)
{
//...

    UIView view = getCurrentView == null
        ? UIApplication.SharedApplication.Windows.Last()
        : getCurrentView.Invoke();

    view.AddSubview(this);
    view.BringSubviewToFront(this);
    view.EndEditing(true);

//...
}