xcodeuidatepickeruibackgroundcolor

UIDatePicker Background isn't changing


I've been trying to change the background && text color for the UIDatePicker, I'm executing this code via ViewDidLoad. The text color changes, but the background color refuses to change. Instead when I run the app and the view loads for the first time, it doesn't change the color, but if I tap inside of the textfield then go to the previous screen then click on the textfield the background color changes like it's supposed to. This is my code for the viewdidload method below:

UIDatePicker *endTimeDatePicker = [[UIDatePicker alloc]init];
endTimeDatePicker.backgroundColor = [UIColor colorWithRed:0/255 
green:0/255 blue:0/255 alpha:0.8];
[endTimeDatePicker setValue:[UIColor whiteColor] forKey:@"textColor"];
[endTimeDatePicker setDate:startTime];
endTimeDatePicker.datePickerMode = UIDatePickerModeDate;
[endTimeDatePicker addTarget:self action:@selector(updateEndTime:) 
forControlEvents:UIControlEventValueChanged];
[promoEndTime setInputView:endTimeDatePicker];

I'm really at a loss, I also tried other variations to set the background color...

setbackground color, set tintcolor, uicolor black

still nothing, still the same result, any assistance would be great thanks.


Solution

  • Normally setting backgroundColor is all that's needed. If you had added the date picker in interface builder, it would work. I suspect that when you call setInputView, your promoEndTime is overriding that setting by changing it to something else.

    You might get around that by setting the background color after calling setInputView. But you might find it better to change your strategy, because date pickers are notoriously resistant to UI changes. You're already on thin ice here with Apple, because the textColor setting is undocumented. Using undocumented API is a major risk of having Apple reject your app from the store. I agree that better appearance customization would be extremely nice here, but changing the text color is asking for trouble from Apple.