I have a UIDatePicker
whose Preferred Style
is set to Compact
and I want to close its calendar popup once the user changes the date. I tried to look for an answer but I can't find anyone talking about it.
Asked
Active
Viewed 886 times
0

Dwigt
- 729
- 6
- 16

Taher El-hares
- 99
- 10
-
If so user may need to open the picker multi times if he want to set the date and time at same time . Actually , we could close the popup by tapping the rest area of the screen . – Lucas Zhang Dec 04 '20 at 12:40
-
I already know this but is there a way to force close the pop-up once the user changes the date? – Taher El-hares Dec 04 '20 at 12:55
-
1Just fyi - this is also addressed in this [post](https://stackoverflow.com/questions/67109078/close-uidatepicker-after-selection-when-style-is-compact?noredirect=1&lq=1) – Dwigt Apr 19 '21 at 11:15
1 Answers
1
If you do want to implement it you need to create a custom Picker .
in UIViewController
UITextField textField;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
textField = new UITextField(new CGRect(10, 100, 200, 80));
textField.TextColor = UIColor.Red;
textField.BackgroundColor = UIColor.LightGray; // you could set the style as you want
// textField.Delegate = new MyPickerDelegate();
UIDatePicker datePicker = new UIDatePicker();
datePicker.BackgroundColor = UIColor.LightGray;
datePicker.ValueChanged += DatePicker_ValueChanged;
if(UIDevice.CurrentDevice.CheckSystemVersion(14,0))
{
datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Inline;
}
else
{
datePicker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
}
textField.InputView = datePicker;
View.AddSubview(textField);
// Perform any additional setup after loading the view, typically from a nib.
}
private void DatePicker_ValueChanged(object sender, EventArgs e)
{
var picker = sender as UIDatePicker;
var date = picker.Date;
var dateFormatter =new NSDateFormatter();
dateFormatter.DateFormat = "YYYY-MM-dd HH:mm";
var dateString = dateFormatter.ToString(date);
textField.Text = dateString;
textField.ResignFirstResponder();
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
textField.ResignFirstResponder();
}

Lucas Zhang
- 18,630
- 3
- 12
- 22