Cross Platform
Android
iOS
 

Create a Dismissible ActionSheet Date Picker

This recipe shows how to pop up an Action Sheet that contains a Date Picker.

Recipe

An Action Sheet is a convenient way to modally request input from the user. This recipe shows you how to use an Action Sheet to allow the user to input a date.

To display an Action Sheet that selects a date:

1. The sample code includes a custom class ActionSheetDatePicker that will be used in this recipe. It contains a custom UIActionSheet implementation that contains a UIDatePicker, UILabel and UIButton.

2. Create the ActionSheetDatePicker and set the Title:

actionSheetDatePicker = new ActionSheetDatePicker (this.View);
actionSheetDatePicker.Title = "Choose Date:";

3. Set the type and validation properties of the UIDatePicker property:

actionSheetDatePicker.DatePicker.Mode = UIDatePickerMode.DateAndTime;
actionSheetDatePicker.DatePicker.MinimumDate = DateTime.Today.AddDays (-7);
actionSheetDatePicker.DatePicker.MaximumDate = DateTime.Today.AddDays (7);

4. Handle the UIDatePicker value changing (in this case, we apply the selected date directly to the UILabel):

actionSheetDatePicker.DatePicker.ValueChanged += (s, e) => {
   dateLabel.Text = (s as UIDatePicker).Date.ToString ();
};

5. In the Choose a date button TouchUpInside handler call Show to display the Action Sheet:

actionSheetDatePicker.Show ();

Additional Information

The sample includes a custom ActionSheetDatePicker class that works by creating a UIActionSheet and adding other subviews directly to it.