UIDatePicker stopped working Xamarin IOS14

Viewed 2250

Since I have updated to Xcode 12 and VS (2019 8.7.8 build 4) on my mac and updated Xamarin to the latest version (Xamarin.iOS 14.0.0.0), I find I am unable to use UIDatePicker.

DatePicker

Looking at the documentation, https://developer.apple.com/documentation/uikit/uidatepicker I need to set Style and maybe preferredDatePickerStyle but neither are these are properties I can set in the (Xamarin) code.

Has anyone found a way to get past this to enable the date to be selected?

4 Answers

It will work once we set the preferredDatePickerStyle and sizeToFit, Sample code is given below.

_datePicker = new UIDatePicker(new CGRect(0, 30, 0, 0));
_datePicker.AutoresizingMask = UIViewAutoresizing.FlexibleRightMargin;
_datePicker.Frame = new CGRect(_datePicker.Frame.Location, new CGSize(300, _datePicker.Frame.Size.Height));
_datePicker.Mode = _datePickerMode;
_datePicker.Date = (NSDate)_defaultDate;
_datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels; //Add this in ios14
_datePicker.SizeToFit(); //Add this in ios14

I have resolved it with this:

_datePicker.SetValueForKey(new NSNumber(1), new NSString("preferredDatePickerStyle"));

It is now showing the correct selector

Date Selector

I'm basically putting together all the details of the other answers into the corresponding implementation in public class CustomDatePickerRenderer : DatePickerRenderer for Xamarin Forms in iOS (omitted my implementation of CreateNativeControl, OnElementPropertyChanged, and custom methods for brevity):

protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
    const int iosVersionMajorForDatePickerStyleFeature = 13;
    const int iosVersionMinorForDatePickerStyleFeature = 4;
    base.OnElementChanged(e);

    if (e.OldElement != null || Element == null)
    {
        return;
    }

    //Switch back to the Wheels style for the datePicker (in iOS 14 the default of Automatic uses the Compact style)
    if (UIDevice.CurrentDevice.CheckSystemVersion(iosVersionMajorForDatePickerStyleFeature, iosVersionMinorForDatePickerStyleFeature)
        && Control?.InputView is UIDatePicker datePicker && datePicker.PreferredDatePickerStyle != UIDatePickerStyle.Wheels)
    {
        datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
        datePicker.SizeToFit();
    }

    //Do whatever other customization is desired
    //UpdateBorderColor();
    //UpdateBorderWidth();
    //UpdateCornerRadius();
    //UpdatePadding();
}

I also encountered this interesting problem recently. When we used Xcode 12 to package it, everything was normal, but when switching to Xcode 12 and above for packaging date selection and display, it would appear abnormal.

Because iOS 13.4 UIDatePicker has new features

  • @property (nonatomic, readwrite, assign) UIDatePickerStyle preferredDatePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

If you want to keep the original style, you can add this code

   _datePick = [[UIDatePicker alloc] init];
   if (@available(iOS 13.4, *)) {
       _datePick.preferredDatePickerStyle = UIDatePickerStyleWheels;
   }
Related