user4034967
user4034967

Reputation:

Xamarin iOS 8 Datepicker

Since I updated to iOS 8 the Datepicker isn't working anymore, because the Actionsheet can't add any Subviews in iOS 8. The reason is because Apple changed the Documentation.

Does anybody know a easy fix to have a Datepicker in iOS 8?

Upvotes: 2

Views: 5629

Answers (2)

user59950
user59950

Reputation: 31

Use a UIAlertController, the preferred replacement for UIActionSheet. Then add the UIDatePicker addSubView to the UIAlertController.view instead of the UIActionSheet. Set the title of the UIAlertController the same as you would have done for the UIActionSheet. Here's a code sample:

    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1){

        //Pre iOS 8
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:title
                                      delegate:self
                                      cancelButtonTitle:nil
                                      destructiveButtonTitle:nil
                                      otherButtonTitles:@"OK", nil];

        [actionSheet addSubview:datePicker];
        [actionSheet setTag:ACTION_SHEET_DATEPICKER];
        [actionSheet showInView:self.mapView];

    } else {

        //for iOS 8

        UIAlertController* datePickerContainer = [UIAlertController alertControllerWithTitle: title
                                                                                     message:nil
                                                                              preferredStyle: UIAlertControllerStyleActionSheet];

        [datePickerContainer.view addSubview:datePicker];

        //Add autolayout constraints to position the datepicker
        [datePicker setTranslatesAutoresizingMaskIntoConstraints:NO];

        // Create a dictionary to represent the view being positioned
        NSDictionary *labelViewDictionary = NSDictionaryOfVariableBindings(datePicker);

        NSArray* hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[datePicker]-|" options:0 metrics:nil views:labelViewDictionary];
        [datePickerContainer.view addConstraints:hConstraints];
        NSArray* vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[datePicker]" options:0 metrics:nil views:labelViewDictionary];
        [datePickerContainer.view addConstraints:vConstraints];

        [datePickerContainer addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
            [self dateSelected:nil];
        }]];

        [self presentViewController:datePickerContainer animated:YES completion:nil];

    }

Please amend and improve as you see fit.

Upvotes: 0

Pete
Pete

Reputation: 4746

Another user isn't using the ActionSheet approach and just using a separate UIViewController to get around this limitation.

Details can be seen on the following post:-

http://forums.xamarin.com/discussion/24128/ios-8-datepicker

There is also some code at:-

https://github.com/SharpMobileCode/ModalPickerViewController

and even a blog post on how to use it:-

http://sharpmobilecode.com/a-replacement-for-actionsheet-date-picker/

Upvotes: 4

Related Questions