3

In my app delegate I set the font and fontColor of all UILabels using [UILabel apperance], but this is causing the font in my UIDatePicker to also change, which Apple don't allow, and for obvious reasons because it makes the datePicker buggy by changing to default font while scrolling and other inconsistent and unintended behaviour.

How do I ignore UIDatePicker to keep it default when setting all UILabels?

DipakSonara
  • 2,598
  • 3
  • 29
  • 34
David P
  • 617
  • 1
  • 15
  • 26

3 Answers3

1

I haven't found a "proper" solution for this problem short of abandoning UIDatePicker or [UILabel appearance], but I did figure out a hack that at least hides the problem. It seems that when the view is initially loaded, the UILabels holding the text for the selected date may be drawn using the label's appearance proxy, but when the UIDatePicker redraws after having the date reset, it will set the label back to the system font.

To hide the problem, in viewWillAppear:, I set the date to two different date values with different date components, then set the picker back to the proper date, like so:

[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:0] animated:NO]; //1970-01-01
[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:49000000] animated:NO]; //1971-07-22
[datePicker setDate:myDate animated:NO]; //Set back to the correct date

It's not elegant, but it makes the date picker look normal again.

1

The only simple work-around I found was to create the UIDatePicker in code (not using storyboards). I got what I wanted.

David P
  • 617
  • 1
  • 15
  • 26
0

Try this ..

[[UILabel appearance] setTextColor:[UIColor redColor]];
[[UILabel appearance] setFont:[UIFont boldSystemFontOfSize:20.0f]];
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setTextColor:nil];// default is nil (text draws black)
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setFont:nil];// default is nil (system font 17 plain)

This will change all labels to red colour and their font except the UIDatePicker one. Hope this helps.

Red colour and bold font are for demonstration purposes. They should be as per your requirement.

Prathamesh Saraf
  • 709
  • 4
  • 14
  • Thanks, but I have tried this and the system font is smaller than the date picker font. I could set the font size to the default UIDatePicker font size but then wouldn't the app get rejected for "changing" UIDatePicker when I'm not supposed to. Also, the months are being slightly obstructed by the background somehow... (Part of weird behaviour). – David P Jun 04 '14 at 08:07
  • I have seen people customise UIDatePicker(Don't know if it was for app store). There is an app which has customised UIDatePicker on the app store referenced from http://stackoverflow.com/questions/10844675/custom-ios-uidatepicker-using-uiappearance. As you can set the date picker to default font, which is nullifying the UILabel change, i guess that should not create a problem for app store. (I encourage others to guide you in this since, i am not much aware of the app store part of it.) – Prathamesh Saraf Jun 04 '14 at 08:21
  • Ok i just remembered, it was not UIDatePicker but UIPicker that had been customised .. my bad.. – Prathamesh Saraf Jun 04 '14 at 08:30
  • Ah no problem, thanks anyway. I'm surprised I haven't found anyone else with this problem... – David P Jun 04 '14 at 08:31