7

I have an issue with date picker, i.e. UIDatePicker is working fine in all version except iOS 8.

Code:

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    [datePicker setDate:[NSDate date]];
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    datePicker.maximumDate=[NSDate date];
    [datePicker addTarget:self action:@selector(updateBirthdateField:) forControlEvents:UIControlEventValueChanged];
    [birthdatField setInputView:datePicker];
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • You can refer a example on how to create/use UIPickerView and UIDatePicker using swift at http://mlk-ios-programming-concepts.blogspot.in/2014/07/hi-all-hope-everyone-is.html – Naga Mallesh Maddali Jul 10 '14 at 13:06
  • I'm seeing intermittent problems too where the date picker sometimes, but not always, disappears and is replaced by a blank view, only on iOS8 and when presented in a popover view. Is this similar to your problem? – Clafou Sep 11 '14 at 14:15
  • No it is due to added in uiactionsheet now i solved it by adding to a sub view because uiaction sheet is now not working in ios 8 version so...! – Praveenkumar_Contus Sep 12 '14 at 07:08
  • You can help from this demo https://github.com/genedelisa/DatePicker – Varsha Vijayvargiya Sep 29 '14 at 10:54

5 Answers5

7

This turned out to be a Simulator issue for me in Xcode 6.0.1. This worked on device (iPhone 4S with iOS 8 release version). As described by the previous user, the steps below made the date picker visible on an iOS 8 simulator for me.

  • Launch the simulator and the app and click on the field that has the date picker as the input view.

  • Now press CMD+K (Hardware->Keyboard->Toggle Software Keyboard) and the date picker became visible.

Again this was a simulator only issue and the issue did not occur on device.

kalyanbk
  • 153
  • 1
  • 7
2

I was seeing same problem with iOS simulator - worked in iOS 7, but not iOS 8. To fix problem, select the Hardware menu for iOS Simulator, then select Keyboard -> Toggle Software Keyboard.

2

I too faced this problem, now i solved this by the following two different methods:

1). Instead of UIActionSheet, i used a customised UIView. Just added the date Pickers to the view and presented and dismissed. Also refer this SWActionSheet.

2). I have checked the conditions that if OS version is greater than 8.0, if NO i used UIActionSheet and else UIAlertViewController. Added the picker controller as subview.

id ageActionSheet;

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];

        ageToolbar.frame = CGRectMake(-8,0, 320, 45);
        ageTitleLbl.frame = CGRectMake(50, 3, 200, 40);
        picker.frame = CGRectMake(-20, 45, 360, frame.size.height - 50 );

        [alertController.view addSubview:ageToolbar];
        [alertController.view addSubview:ageTitleLbl];
        [alertController.view addSubview:picker];

        ageActionSheet = alertController;
    }
    else
    {
        ageActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles: nil];
        [ageActionSheet addSubview:ageToolbar];
        [ageActionSheet addSubview:ageTitleLbl];
        [ageActionSheet addSubview:picker];
        [ageActionSheet sizeToFit];
    }
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
2

Since UIActionSheet is deprecated in iOS8, you have to use some other view to hold the UIDatePicker and UIToolBar... In this answer I've used UIView to hold the above mentioned UI objects.

@interface ViewController ()<UIActionSheetDelegate, UITextFieldDelegate>{ 

   UIActionSheet *pickerViewActionSheet;
   UIDatePicker *datePicker;
   UIToolbar *pickerToolbar;       
   UIView *dateView;
}
@property (strong, nonatomic) IBOutlet UITextField *dateOfBirthTextField;
@end


@implementation ViewController

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if(textField == self.dateOfBirthTextField){        
    [textField resignFirstResponder];
    float platformVersion = 8.0;
    //Check for iOS 8.0+
    if([[[UIDevice currentDevice] systemVersion] floatValue ] >= platformVersion){

        // iOS8 DatePicker view implementation
        dateView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2.0, 320, 640)];
        pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        pickerToolbar.barStyle=UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dateChosen:)];
        [barItems addObject:barButtonItem];
        barButtonItem.tag = 123;
        [pickerToolbar setItems:barItems animated:YES];
        [dateView addSubview:pickerToolbar];
        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 300.0)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];            
        datePicker.backgroundColor = [UIColor whiteColor];
        [dateView addSubview:datePicker];


        [self.view addSubview:dateView];

    }
    else { 
        //Pre iOS8.0 "usual" DatePicker implementation.           
        datePicker = [[UIDatePicker alloc] init];

        pickerViewActionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:nil];

        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 300.0)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
        datePicker.tag = -1;
        pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        pickerToolbar.barStyle=UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *batButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)];
        [barItems addObject:batButtonItem];

        [pickerToolbar setItems:barItems animated:YES];
        [pickerViewActionSheet addSubview:pickerToolbar];
        [pickerViewActionSheet addSubview:datePicker];
        [pickerViewActionSheet showInView:self.view];
        [pickerViewActionSheet setBounds:CGRectMake(0,0,320, 464)];

       }

    }
}

//iOS8.0+ when the date has chosen, remove the corresponding view from the parent view.
-(void) dateChosen:(UIBarButtonItem *) barButton {
     if(barButton.tag ==123){
        [dateView removeFromSuperview];

     }

  }

 //Get the chosen date from the UIDatePicker to the corresponding UITextField.
 -(void)dateChanged{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"dd/MM/yyyy"];
     self.dateOfBirthTextField.text = [dateFormatter stringFromDate:[datePicker date]];
  }

 //Pre iOS8.0 for closing the actionsheet
 -(BOOL)closeDatePicker:(id)sender{
     [pickerViewActionSheet dismissWithClickedButtonIndex:0 animated:YES];
     [self.dateOfBirthTextField resignFirstResponder];
     return YES;
  }

 //Pre iOS8.0
 -(void)doneButtonClicked{
     [self closeDatePicker:self];
  }
 @end

I hope this helps....

Suresh Kumar Durairaj
  • 2,104
  • 16
  • 24
0

Its Nothing Just On Toggle Software Keyboard.. in

When Opening in simulator Just Go to ..

Hardware >> Keyboard >> Toggle SOftware Keyboard...
Sumit Sharma
  • 443
  • 3
  • 8