views:

3625

answers:

4

Hi All,

Does anyone know how to change the color of a row (or row background) in the UIPickerView control from the iPhone SDK? Similiar to the below title for row, however I would also like to change the color of the row:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

Thank you.

+6  A: 

You can return an arbitrary view in the delegate's -pickerView:viewForRow:forComponent:reusingView: method, documented here.

Noah Witherspoon
+7  A: 

Thanks Noah, that's exactly what I needed. I wanted to add the code here just in case anyone else needs (or wants to comment on :)

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

    CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
    UIImageView *label = [[UIImageView alloc] initWithFrame:imageFrame];

    if (row == 0)
    {
     label.backgroundColor = [UIColor redColor];
    }
    if (row == 1)
    {
     label.backgroundColor = [UIColor blueColor];
    }
    if (row == 2)
    {
     label.backgroundColor = [UIColor blackColor];
    } 
    return label;
}
Sean
Shouldn't you use the `reusingView` parameter ? It is similar to UITableView's reuse pool...
François P.
Is there a way to customize the selection bar which is shown as gray gradient in a UIPickerView?
Raj
A: 

Do this

{code} label.backgroundColor = [UIColor yourcolorColor]; {code}

A: 

Could someone please explain how to do this from an IBAction, ie. selectively switch color when a row is selected.

TrueScot