Xamarin.ios在AlertController中添加DatePickerView和PickerView

时间:2018-01-18 13:25:49

标签: c# xamarin xamarin.ios uipickerview uidatepicker

有可能将DatePicker和PickerView放在xamarin.ios中的Alert控制器中吗? Example image

我试过这个:

UIDatePicker datePickerView = new UIDatePicker(new CGRect(0, 0, this.View.Frame.Width, 300));
        var calendar = new NSCalendar(NSCalendarType.Gregorian);
        var currentDate = NSDate.Now;
        var components = new NSDateComponents();

        components.Year = -60;

        NSDate minDate = calendar.DateByAddingComponents(components, NSDate.Now, NSCalendarOptions.None);

        datePickerView.MinimumDate = minDate;
        datePickerView.MaximumDate = NSDate.Now;
        datePickerView.Mode = UIDatePickerMode.Date;


        UIAlertController actionSheetAlert = UIAlertController.Create("Data di nascita", "Seleziona un'opzione dalla lista", UIAlertControllerStyle.ActionSheet);


        actionSheetAlert.View.AddSubview(datePickerView);

        actionSheetAlert.AddAction(UIAlertAction.Create("Aggiungi", UIAlertActionStyle.Default, (action) => {

        }));

        actionSheetAlert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Destructive, (action) => Console.WriteLine("Cancel button pressed.")));


        this.PresentViewController(actionSheetAlert, true, null);

这是结果: Result image

你能提供一个完整的例子吗?感谢!!!!

2 个答案:

答案 0 :(得分:0)

来自Apple Documentation

  

UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

您可以使用的是一个自定义控制器或视图,您将从头开始构建,您可以模态显示和解除并完全控制。

我认为你可以在AlertController上获得的最高级别的自定义是你可以在这里找到的:

Tutorial to add textfields / buttons on alert

答案 1 :(得分:0)

我认为我们可以创建自己的UITextField来实现屏幕截图的效果,这是我的自定义textField,您可以参考:

public class MyTextField : UITextField
{
    private UIDatePicker inputView;
    private UIView inputAccessoryView;
    public override UIView InputView
    {
        get
        {
            if (inputView == null)
            {
                UIDatePicker datePickerView = new UIDatePicker();
                var calendar = new NSCalendar(NSCalendarType.Gregorian);
                var currentDate = NSDate.Now;
                var components = new NSDateComponents();

                components.Year = -60;

                NSDate minDate = calendar.DateByAddingComponents(components, NSDate.Now, NSCalendarOptions.None);

                datePickerView.MinimumDate = minDate;
                datePickerView.MaximumDate = NSDate.Now;
                datePickerView.Mode = UIDatePickerMode.Date;

                inputView = datePickerView;
            }
            return inputView;
        }
    }

    public override UIView InputAccessoryView
    {
        get
        {
            if (inputAccessoryView == null)
            {
                UIToolbar toolbar = new UIToolbar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 44));
                UIBarButtonItem item = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, args) =>
                {
                    NSDateFormatter formatter = new NSDateFormatter();
                    formatter.DateFormat = "dd/MM/yyyy HH:mm";
                    this.Text = formatter.ToString(inputView.Date);
                    this.ResignFirstResponder();
                });
                UIBarButtonItem flexibleItem = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
                toolbar.SetItems((new UIBarButtonItem[] { flexibleItem, item }), false);

                inputAccessoryView = toolbar;
            }
            return inputAccessoryView;
        }
    }
}

覆盖InputView(将你的datePicker放入其中)和InputAccessoryView(带有&#34的工具栏;完成")

最后我们可以像:

一样使用它
MyTextField textField = new MyTextField();
textField.Placeholder = "Enter something";
textField.Frame = new CGRect(0, 100, 200, 200);
View.AddSubview(textField);