4

I want to change the background color in UIPickerView, Is it possible?

hasan
  • 23,815
  • 10
  • 63
  • 101
MD.
  • 1,131
  • 4
  • 18
  • 28

4 Answers4

7

UPDATE: One simple trick is to set the alpha on some of the subviews. Which one specifically depends on how many components you have, but the code is:

[(UIView*)[[picker subviews] objectAtIndex:2] setAlpha:0.5f];

This will make the dial semi transparent and show through the background colour.


Setting the backgroudColor doesn't appear to do anything. The best I have been able to do so far is set the background colour of individual component rows, which is next to useless as the base colour of the component will remain white.

Ideally I'd like to set the background colour of an each component (i.e. dial).

detunized
  • 15,059
  • 3
  • 48
  • 64
Ryan Booker
  • 433
  • 3
  • 9
3

Finally managed to do it. I try it for a picker view with one component.

  1. set the picker view background color to clear color by wizard or by code as follow:

    Picker1.backgroundColor = [UIColor clearColor];
    
  2. Set alpha value to 0.0f for the picker view subviews numbered 0, 1 and 3. (Don't know why there is subview 2 thought). Do it by code after the first load data for the picker view as follow (it will throw an exception if you do this in the DidViewLoad).

    [(UIView*)[[Picker1 subviews] objectAtIndex:0] setAlpha:0.0f];
    [(UIView*)[[Picker1 subviews] objectAtIndex:1] setAlpha:0.0f];
    [(UIView*)[[Picker1 subviews] objectAtIndex:3] setAlpha:0.0f];
    
  3. Don't forget to clear background color for the label you are sending to the picker view in the viewForRow method.

    lbl.backgroundColor = [UIColor clearColor];
    
j0k
  • 22,600
  • 28
  • 79
  • 90
hasan
  • 23,815
  • 10
  • 63
  • 101
3

Seems iOS 4.2 doesn't has any subview...

NSLog(@" %@ ", [self.pickerView subviews]); 

returns empty array.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Saturn
  • 151
  • 1
  • 7
  • 2
    It will give subviews in pickerview delegates -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ – Priyanka V Jun 15 '12 at 08:56
1

On iOS 7 I just drop a custom background image behind it. Didn't have to change anything else.

CommaToast
  • 11,370
  • 7
  • 54
  • 69
  • 1
    +1 for the iOS7 update. With your info giving the columns different background was easy - I just created a fitting stripe background with Photoshop. Some white fading at the upper and lower border. Done! You can see it here: http://stackoverflow.com/questions/20887001 – DerWOK Jan 06 '14 at 10:00