Xamarin从Image Tap显示iOS UIDatePicker

时间:2017-09-09 15:13:00

标签: c# xamarin xamarin.ios uidatepicker

所以我正在使用Xamarin并尝试创建一个图像渲染器,在点击时会显示iOS的UIDatePicker。我让渲染器适用于Android,但我无法在iOS上显示日期选择器。我的代码在下面是渲染器,我调试了它,我一直通过mTextField.BecomeFirstResponder();我“认为”会使日期选择器显示。

有人能指出我正确的方向吗?我只需要在屏幕上显示该选择器。一旦我可以显示它,我可以像我想要的那样处理它。

private void _DatePickerHasBeenTapped( object sender, EventArgs e )
    {
        if( mDatePickerDialog != null )
        {
            mDatePickerDialog.TouchCancel -= _DatePickerDialogOnCancelEvent;
            mDatePickerDialog.ValueChanged -= _DatePickerDialogOnValueChanged;
        }

        var date = mDatePicker.PickerDate == DateTime.MinValue
            ? DateTime.Today
            : mDatePicker.PickerDate;


        mTextField = new UITextField();

        mDatePickerDialog = new UIDatePicker { Mode = UIDatePickerMode.Date, TimeZone = new NSTimeZone( "UTC" ) };

        mDatePickerDialog.ValueChanged += _DatePickerDialogOnValueChanged;
        mDatePickerDialog.SetDate( date.ToNSDate(), false );

        var width = UIScreen.MainScreen.Bounds.Width;
        var toolbar = new UIToolbar( new RectangleF( 0, 0, (float)width, 44 ) ) { BarStyle = UIBarStyle.Default, Translucent = true };
        var spacer = new UIBarButtonItem( UIBarButtonSystemItem.FlexibleSpace );
        var doneButton = new UIBarButtonItem( UIBarButtonSystemItem.Done, ( o, a ) => mDatePickerDialog.ResignFirstResponder() );

        toolbar.SetItems( new[] { spacer, doneButton }, false );

        mTextField.InputView = mDatePickerDialog;
        mTextField.InputAccessoryView = toolbar;

        if( mTextField.CanBecomeFirstResponder )
        {
            mTextField.BecomeFirstResponder();
        }
    }

1 个答案:

答案 0 :(得分:2)

如果实际显示UITextField,您的方法将有效。现在,您可以通过将UITextField添加到VC视图层次结构中来 hack 它,但是使用负cgrect将其偏移以将其从屏幕上移除并在UIBarButtonItem处理程序中删除它之后:

var mTextField = new UITextView(new CGRect(-1000, -1000, 10, 10));
yourViewController.Add(mTextField);

注意:我已经看到了屏幕控制下的响应器故障并且当我需要它们作为虚假响应者时避免它们,但我知道其他人这样做...用户要小心......: - /

或者:

您可以通过编程方式或从包含选择器和关闭按钮的故事板设计UIViewController,并在用户点击图像时显示。在关闭按钮上点击,关闭控制器并将选择器日期发送到某个地方......

或者:

对于像这样的东西,我通常使用一个popover样式的视图控制器。

实施例

var parentVC = this; // This would the "main" Form's ViewController, obtain it within your renderer

PopoverDelegate popoverDelegate = null;
var datePicker = new UIDatePicker(new CGRect(0, 0, parentVC.View.Frame.Width, 300));
popoverDelegate = new PopoverDelegate(datePicker, (date) =>
{
    // Post the date change back to Forms via an event, etc... 
    Console.WriteLine($"Date Choosen: {date}");
    datePicker.Dispose();
    popoverDelegate.Dispose();
});
using (var popOverStyleVC = new UIViewController
{
    ModalPresentationStyle = UIModalPresentationStyle.Popover
})
{
    var pc = popOverStyleVC.PopoverPresentationController;
    {
        pc.Delegate = popoverDelegate;
        pc.SourceView = parentVC.View;
        pc.SourceRect = imageButton.Frame; // a CGRect where you want the popup arrow to point
    }
    popOverStyleVC.PreferredContentSize = datePicker.Frame.Size;
    popOverStyleVC.View.Add(datePicker);
    parentVC.PresentViewController(popOverStyleVC, true, null);
}

结果:

enter image description here

{strong} >正确显示,这也是我解雇并提供日期回调的地方。

UIPopoverPresentationControllerDelegate:

UIPopoverPresentationControllerDelegate