Forum Xamarin.Forms

Custom Control for iOS - Hour and minute selector.

Hello,
I am working on an app that requires the user to select the time for how long something took. Specifically the hours and minutes. I saw that in iOS the UIDatePicker has a mode called "countDownTimer". However creating a custom renderer for time picker and setting the mode property will cause a argument exception when the user sets the minutes. It thinks the Time property is incorrect.

Is there a simple way to do this or not really? I believe it would be possible to create a custom entry where the InputView is set to a UIDatePicker but i am unsure how to ensure values make it correctly between the picker, binding, and entry field (while converters are still being applied).

Here is some of the code I have tried.

[assembly: ExportRenderer(typeof(HourMinutePicker), typeof(HourMinutePickerRenderer))]
namespace XamarinFormsPlayground.iOS
{
    public class HourMinutePickerRenderer : TimePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var picker = Control.InputView as UIDatePicker;

                if (picker != null)
                {
                    picker.Mode = UIDatePickerMode.CountDownTimer;

                }
            }
        }
    }
}

Thanks,
Timko

Posts

  • Jesse_JiangJesse_Jiang USMember ✭✭✭

    I think you can try bindable picker to binding a list of strings like ["10 mins", "20 mins", "1 hours"].

    You can find the bindable picker from FreshEssentials https://github.com/XAM-Consulting/FreshEssentialsSample

  • BalakrishnanSBalakrishnanS USMember ✭✭

    I am also facing the same issue if we set the picker mode as count down timer.
    anybody found a solution.

  • Naelan26Naelan26 Member ✭✭

    @BalakrishnanS said:
    I am also facing the same issue if we set the picker mode as count down timer.
    anybody found a solution.

    also search a solution

  • BalakrishnanSBalakrishnanS USMember ✭✭

    Hi

    I found the reason for this.

    To implement the countDownTimer you have to use the DatePicker not Time picker.
    public class HourMinutePickerRenderer : DatePickerRenderer

    also you have to use the DatePicker control.

    Thanks
    Balakrishnan.S

    @mr.timko said:
    Hello,
    I am working on an app that requires the user to select the time for how long something took. Specifically the hours and minutes. I saw that in iOS the UIDatePicker has a mode called "countDownTimer". However creating a custom renderer for time picker and setting the mode property will cause a argument exception when the user sets the minutes. It thinks the Time property is incorrect.

    Is there a simple way to do this or not really? I believe it would be possible to create a custom entry where the InputView is set to a UIDatePicker but i am unsure how to ensure values make it correctly between the picker, binding, and entry field (while converters are still being applied).

    Here is some of the code I have tried.

    [assembly: ExportRenderer(typeof(HourMinutePicker), typeof(HourMinutePickerRenderer))]
    namespace XamarinFormsPlayground.iOS
    {
      public class HourMinutePickerRenderer : TimePickerRenderer
      {
          protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
          {
              base.OnElementChanged(e);
    
              if (e.NewElement != null)
              {
                  var picker = Control.InputView as UIDatePicker;
    
                  if (picker != null)
                  {
                      picker.Mode = UIDatePickerMode.CountDownTimer;
    
                  }
              }
          }
      }
    }
    

    Thanks,
    Timko

Sign In or Register to comment.