Pugalmuni
Pugalmuni

Reputation: 9390

How to change the Background color of UIPickerView in iPhone

I have used picker view controller in my application. Now i want to change the color of the background color. So how can i change the background color of picker view and want to change the row background color also. I want to change the default layout color of picker view and Is any possible to set the custom image as background of picker view?. So please guide me.

Upvotes: 4

Views: 5312

Answers (5)

Nirzar Gandhi
Nirzar Gandhi

Reputation: 79

i have changed UIPicker background color and UIPicker toolbar color and font color. Let give your UIPickerView name as pickerView.

pickerView.backgroundColor=[UIColor greenColor]; - At place of greenColor you can use any color

pickerView.backgroundColor=[UIColor colorWithRed: 127 green:127 blue:127 alpha:1]; - Here you can give any color code of your choose.

Suppose you taken toolbar in UIPickerView then we can change color using toolBar.backgroundcolor=[UIColor greenColor]; - At place of greenColor you can use any color

toolBar.backgroundColor=[UIColor colorWithRed: 127 green:127 blue:127 alpha:1]; - Here you can give any color code of your choose.

toolBar.backgroundImage=[UIImage imageWithName:@"imageName"]; - You can set image as backgroundImage in toolbar.

Upvotes: 0

Mark Granoff
Mark Granoff

Reputation: 16938

I wrote an article about just this topic recently. Pretty easy to achieve what you want to do. (While the article is iOS7 focused, the technique of supplying views rather than strings for your component rows works regardless of modern iOS version.)

Upvotes: 0

hasan
hasan

Reputation: 24195

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

set the picker view background colour by wizard or by code as follow:

Picker1.backgroundColor = [UIColor someColor];// clearColor for transparent

Set alpha value 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:someValue];// 0.0f for transparent
[(UIView*)[[Picker1 subviews] objectAtIndex:1] setAlpha:someValue];
[(UIView*)[[Picker1 subviews] objectAtIndex:3] setAlpha:someValue];

Don't forget to clear background color for the label you are sending to the picker view in the viewForRow method.

lbl.backgroundColor = [UIColor someColor];// could be clearColor

Upvotes: 0

Leon Deriglazov
Leon Deriglazov

Reputation: 1132

I already answered a similar question. You can change the background color by completely replacing background image using IXPickerOverlayView class (available on GitHub).

Upvotes: 1

EvilPenguin
EvilPenguin

Reputation: 525

you can add a UIView to the components of the UIPickerView, but you cannot change the background color or the tint color.

  • viewForRow:forComponent is for the views.

Upvotes: 0

Related Questions