代码之家  ›  专栏  ›  技术社区  ›  Stefano Toppi

沙马林。ios在AlertController中添加DatePickerView和PickerView

  •  0
  • Stefano Toppi  · 技术社区  · 7 年前

    可以在xamarin的警报控制器中放置日期选择器和PickerView。像这样的ios? 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 回复  |  直到 7 年前
        1
  •  1
  •   Ax1le    7 年前

    我认为我们可以创造自己的 UITextField 为了实现屏幕截图的效果,这里是我的自定义文本字段,供您参考:

    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 (把你的日期选择器放进去)然后 InputAccessoryView (带有“完成”的工具栏)

    最后,我们可以像这样使用它:

    MyTextField textField = new MyTextField();
    textField.Placeholder = "Enter something";
    textField.Frame = new CGRect(0, 100, 200, 200);
    View.AddSubview(textField);
    
        2
  •  0
  •   Gil Sand    7 年前

    Apple Documentation :

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

    您可以使用的是一个自定义控制器或视图,您将从头开始构建它,您可以通过模式显示和关闭它并完全控制它。

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

    Tutorial to add textfields / buttons on alert