1

How can I set a green color in UIDatePicker's selected row. I had searched and put some kind of code like below,

The first one is:

UILabel *label = [UILabel appearanceWhenContainedIn:[UITableView class],[UIDatePicker class], nil];
label.font = HELVETICA_NEUE(24);
label.textColor = GREEN_COLOR;

Here, the green color is only set on the date and month, but not the year. And the other non-selected dates in the picker should still show up black.

I just want the selected row to be green.

Next, I followed the Aron post in below link:

can I change the font color of the datePicker in iOS7?

Then I create the UILabel + UIDatePickerLabel class file in my project.

ALL the dates in the UIDatePicker showed up green, and I need only the selected row to be green. Does anyone know how to do this?

Community
  • 1
  • 1
Ram
  • 764
  • 1
  • 7
  • 20

3 Answers3

1
UIView *overIndicoloe = [[UIView alloc] initWithFrame:CGRectMake(20, 130, 280, 44)];
overIndicoloe.backgroundColor = [UIColor greenColor];
overIndicoloe.alpha = 0.5f;
[yourDatePicker addSubview: overIndicoloe];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • u need to change the font color or selection indicator of the row color – Anbu.Karthik May 28 '14 at 12:38
  • Thanks for your reply. Yes I need to change selected row text color but your code only display the green background color of the UIView. – Ram May 28 '14 at 12:54
0

if u need to change the text color

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
    label.textColor = [UIColor greenColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    label.text = [NSString stringWithFormat:@" %d", row];
    return label;    
    }
Agent Chocks.
  • 1,312
  • 8
  • 19
  • Would be fine, if `UIDatePicker` would be an element of `UIPickerView`. better see http://stackoverflow.com/a/21084227/883799 or http://stackoverflow.com/a/20875372/883799 ;) – geo Nov 27 '14 at 14:40
0

You can also change just the selection indicator's background color now. Just add a UIView above the selection indicator, set it's alpha value low(depends on you, I like my overlay to look transparent), give it a background color and you are good to go.

Consider this,

var overlayOnPicker:UIView = UIView(frame: CGRectMake(1, 68, mypicker.frame.width, 26))
// Adds a layer on the selection indicator

And do put the CGRect's X value=1 (remember, it's a frame, so it will be placed according to the superview's coordinate system)

overlayOnPicker.backgroundColor = UIColor.whiteColor()
overlayOnPicker.alpha = 0.2
myDatePicker.addSubview(overlayOnPicker)
//You have to add the overlayOnPicker view as a subview to the Date Picker.
//myDatePicker is the UIDatePicker that I declared earlier in my code

Still figuring out how to change text's color in date picker

Zoran777
  • 564
  • 2
  • 7
  • 25