我认为我们可以创造自己的
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);