Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIPickerView background

I want to change the border color of a UIPickerView. I do not see a tint property for the UIPickerView. Is there any way this could be done? Or a workaround?

Thanks.

like image 582
lostInTransit Avatar asked Jun 08 '09 15:06

lostInTransit


4 Answers

You could also mask the component. With a bit fiddeling you can get the size of the component and cut it out with following code:

CALayer* mask = [[CALayer alloc] init];
        [mask setBackgroundColor: [UIColor blackColor].CGColor];
        [mask setFrame:  CGRectMake(10.0f, 10.0f, 260.0f, 196.0f)];
        [mask setCornerRadius: 5.0f];
        [picker.layer setMask: mask];
        [mask release];

Don't forget

#import <QuartzCore/QuartzCore.h>
like image 150
Quxflux Avatar answered Oct 28 '22 20:10

Quxflux


If you just want a workaround, take a screen shot in the simulator, open it in photoshop, crop it to just the UIPickerView area, make the center transparent, apply whatever tint you want, add that image to your project, and add it as a UIImageView on top of the UIPickerView.

like image 38
Ed Marty Avatar answered Oct 28 '22 21:10

Ed Marty


My UIPickerView has 3 components. And no Selection Indicator.

This gives it 11 subviews. [[picker subviews] count]

Hiding the first and the last subview totally removes the background.

[(UIView*)[[picker subviews] objectAtIndex:0] setHidden:YES];
[(UIView*)[[picker subviews] objectAtIndex:10] setHidden:YES];

Hiding every third other subview (indexes 1, 4 and 7) hides the opaque background on the components. Giving quite a nice effect that I can skin as I desire.

Hope that helps someone :)

like image 14
adam Avatar answered Oct 28 '22 21:10

adam


The picker will not have subviews until it is fully loaded. If you try to do this:

[(UIView*)[[picker subviews] objectAtIndex:0] setHidden:YES];
[(UIView*)[[picker subviews] objectAtIndex:10] setHidden:YES];

in the viewDidLoad or viewWillAppear it will not work. However, I moved mine into one of the UIPickerView protocol methods and it removed the background frame.

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (self.pickerBackgroundNotHidden)
    {
        self.pickerBackgroundNotHidden = false;
        [(UIView*)[[tempPicker subviews] objectAtIndex:0] setHidden:YES];
        [(UIView*)[[tempPicker subviews] objectAtIndex:7] setHidden:YES];
        [(UIView*)[[tempPicker subviews] objectAtIndex:8] setHidden:YES];
    }
    return [arrayColors objectAtIndex:row];
}

You could probably subclass the picker to do this a little more efficiently, but I prefer to avoid subclassing.

Oh, also, this is probably obvious, but if your picker doesn't have any items in it, the code above will not delete the sub views.

like image 5
Matt Hudson Avatar answered Oct 28 '22 20:10

Matt Hudson