blank UIDatePicker on iPad in mode UIDatePickerMod

2019-02-25 19:29发布

问题:

I am struggling with the UIDatePicker on iPad. The setDate or .date assignment makes the DatePicker empty:

    UIDatePicker * dtVCpicker = [[UIDatePicker alloc] initWithFrame:CGRectZero]; 
    dtVCpicker.datePickerMode = UIDatePickerModeDateAndTime;
    dtVCpicker.date = [NSDate date];
    dtVCpicker.minimumDate = [NSDate date];
[self.view.window addSubview:dtVCpicker];

if I change the mode to UIDatePickerModeDate or UIDatePickerModeTime then it would look normal.

Thanks a lot for your answers!

回答1:

The problem is with the line:

dtVCpicker.date = [NSDate date];

This causes the UIDatePicker with UIDatePickerModeDateAndTime to break (probably a bug). However, this line is needless, since the date picker is by default set to current date.

To sum up, in my code I simply solve the issue by removing the above line. Of course, it is still possible to set the date of the picker, but in the other method.



回答2:

I implemented the work-around for this issue: setting the dtVCpicker.date inside the viewDidLoad made the Picker displayable.


In my existing code, I just added the picker inside a Popup, and so viewDidLoad wasn't called when the Popup displays. So I created a view that contains the picker, and put the view inside the Popup (and put the setDate in the viewDidLoad). It works.