Customizing UIPickerView (background and spacing) - iphone

I would like to change the white background in a UIPickerView to an image of my own.
Is this possible?
Also, I have managed to get my UIPickerView to scroll horizontally instead of vertical. Now, I would like to know if there is any way to adjust the spacing between two rows of the picker view?
I have attached an image to show what I mean.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrayDays = [[NSMutableArray alloc] init];
[arrayDays addObject:#"ONSDAG"];
[arrayDays addObject:#"TORSDAG"];
[arrayDays addObject:#"FREDAG"];
[arrayDays addObject:#"LØRDAG"];
arrayDates = [[NSMutableArray alloc] init];
[arrayDates addObject:#"29. JUNI"];
[arrayDates addObject:#"30. JUNI"];
[arrayDates addObject:#"1. JULI"];
[arrayDates addObject:#"2. JULI"];
pickerViewDay = [[UIPickerView alloc] initWithFrame:CGRectZero];
[pickerViewDay setDelegate:self];
[pickerViewDay setShowsSelectionIndicator:NO];
CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2);
rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
[pickerViewDay setTransform:rotate];
[pickerViewDay setCenter:CGPointMake(self.view.frame.size.width/2, (pickerViewDay.frame.size.height/2)-3)];
[self.view addSubview:pickerViewDay];
// Adding selection indicator to pickerview
UIImage *selectorImage = [UIImage imageNamed:#"DayPickerView_SelectionIndicator.png"];
UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
[customSelector setFrame:CGRectMake(0, 0, 120, 74)];
[customSelector setCenter:CGPointMake(self.view.frame.size.width/2, customSelector.frame.size.height/2)];
[self.view addSubview:customSelector];
[customSelector release];
// Adding background to pickerview
UIImage *backgroundImage = [UIImage imageNamed:#"DayPickerView_Background.png"];
UIView *custombackground = [[UIImageView alloc] initWithImage:backgroundImage];
[custombackground setFrame:CGRectMake(0, 0, 320, 74)];
// [self.view addSubview:custombackground];
[custombackground release];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
// Date
CGRect rectDate = CGRectMake(30, 0, 150, 80);
UILabel *date = [[UILabel alloc]initWithFrame:rectDate];
[date setTransform:rotate];
[date setText:[arrayDates objectAtIndex:row]];
[date setFont:[UIFont fontWithName:#"Arial-BoldMT" size:37.0]];
[date setShadowColor:[UIColor whiteColor]];
[date setShadowOffset:CGSizeMake(0, -1)];
[date setTextAlignment:UITextAlignmentCenter];
[date setBackgroundColor:[UIColor clearColor]];
[date setClipsToBounds:YES];
[viewRow addSubview:date];
// Day
CGRect rectDay = CGRectMake(-30, 0, 150, 80);
UILabel *day = [[UILabel alloc]initWithFrame:rectDay];
[day setTransform:rotate];
[day setText:[arrayDays objectAtIndex:row]];
[day setFont:[UIFont fontWithName:#"Arial-BoldMT" size:21.0]];
[day setTextColor:[UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1]];
[day setTextAlignment:UITextAlignmentCenter];
[day setBackgroundColor:[UIColor clearColor]];
[day setClipsToBounds:YES];
[viewRow addSubview:day];
return viewRow;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayDays objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayDays count];
}
EDIT 1
For RickiG (on background):
EDIT 2
For RickiG:

Hi
There is no direct way to change the background. What you can do is to have the view you return in viewForRow feature its own background (then add the shadow in each side afterwards if you need it). You can also go looking for viewWithTag: but that is never a good idea as this might change in future iOS releases.
Is there a special reason you implement both viewForRow and TitleForRow? I usually just populate the viewForRow's labels inside this delegate method.
The viewForRow has the ability to reuse the views in the Picker, much like a UITableView you should test if the "reusingView:(UIView *)view" is nil and if not there is no need to draw everything again. Just populate the labels.
I usually never customize the picker, if I need something not completely custom I subclass the UITableView, it is much more flexible and can what the Picker does + more.
For the spacing you can use the "height" of the rows, the picker will center the views you return in viewForRow, then just make sure:
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
returns a value bigger than your view.
Held og lykke;)

I just figured out how to apply background color or image in the picker view. Hope it may help someone.
Just define a category in the file where you want to you picker view as follow:
#implementation UIPickerView(Extension)
-(void)drawRect:(CGRect)rect
{
NSArray* subviews = [self subviews];
for(UIView* view in subviews)
{
if([view isKindOfClass:[UITableView class]])
{
view.backgroundColor = appColor;
}
}
[super drawRect:rect];
}
#end
You can look further at subviews for more customization.

Looks like this is an old thread, but in iOS 7 (Possibly earlier, I'm not sure), UIPickerViews have a background color property, so setting the background color is simple:
[pickerView setBackgroundColor:[UIColor whiteColor]];
Setting a background image is similarly simple thanks to [UIColor colorWithPatternImage:]. (Note that the pattern will tile if the image is smaller than the UIPickerView.)
[pickerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"someBackgroundImage.png"]]];
To modify the component widths you can implement the delegate method widthForComponent in order to set various widths for each component. In theory, you should be able to add an empty component and use it's blank width to create spacing, but I haven't tried this.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
I haven't been able to find a way to change the padding between components which seems like a nicer way to modify the spacing and would also allows you to remove the ~5 px spacing between components, but if I'm able to find one I will update my answer.

How about adding an empty view on the right of your labels? And for the background you should probably use insertSubview:aboveView: method with the default background view as the second parameter (if you can access to it).

Related

iOS6 UIPickerView memory leak issue.

I was getting this memory leak:
[UIPickerTableViewTitleCell initWithStyle:resuableIdentifier];
and
NSConcentrateMutableAttributedString.
Issue was that I had not implemented this delegate. After implementing this now memory leaks goes away. May be this information helpful for other as I spend mine 16 hours only to figure out this issue.
// Do something with the selected row.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
// Get the text of the row.
NSString *rowItem = [NSString stringWithFormat:#" %#",[machineData objectAtIndex:row]];
// Create and init a new UILabel.
// We must set our label's width equal to our picker's width.
// We'll give the default height in each row.
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
// Make the text color red.
[lblRow setTextColor: [UIColor blackColor]];
[lblRow setFont:[UIFont boldSystemFontOfSize:20]];
// Center the text.
[lblRow setTextAlignment:UITextAlignmentLeft];
// Add the text.
[lblRow setText:rowItem];
// Clear the background color to avoid problems with the display.
[lblRow setBackgroundColor:[UIColor clearColor]];
// Return the label.
return lblRow;
}
Thanks for your info. Was confused by this leak. Only few comments:
probably lblRow should be autoreleased: return [lblRow autorelease];
[pickerView rowSizeForComponent:component] can be used to get size for new label.
I used IUIPicker in a popover and every time I dismissed the popover I had memory leak. I am also using ARC, so the easiest way I resolved this was by setting the UIPickerView = nil on unload. The following appears to have done the trick.
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.pickerView = nil;
}

Size of UIPickerView is not set correctly

I need to show a view that is not full screen and has a button and pickerView under it.
I tried using this code:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20,20,200,200)];
container.backgroundColor=[UIColor whiteColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y+5, 170, 20); // position in the parent view and set the size of the button
myButton.titleLabel.textColor=[UIColor redColor];
myButton.titleLabel.text=#"click me";
//myButton.backgroundColor=[UIColor blueColor];
//[myButton backgroundImageForState:<#(UIControlState)#>[UIImage imageNamed:#"iPhone_mainbutton_green.png"];
// add targets and actions
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add a buttonview
[container addSubview:myButton];
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
//piker.numberOfComponents=1;
piker.showsSelectionIndicator=YES;
//piker.delegate=self;
//piker.dataSource=self;
[container addSubview:piker];
[myButton release];
[piker release];
[self.view addSubview:container];
and i get this (picker out of the screen and very large, not 100x100):
you are adding you pickeer view in container and container frame is:-(20,20,200,200)
make it(0,20,200,200).
You are adding your UIPickerView as subview of "container"
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
this means pickerview takes its origin from container not from UIView if you want it to on right position do this :-
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
[container addSubview:picker];
just focus on your origin and remember every subview takes it origin from its parent view.
and One more thing apple doesn't allow to change pickerview Height so you cant set it 100x100 you can just change of its width. UIPickerView supports only 3 height value and these are 216.0 , 180.0, 162.0 try to set height only from these 3 value. it will be work.
let me know if you have any query regarding this.
If you want to adjust the frame, do it in your view controller's viewWillAppear: method - you can adjust it there.
Make sure that you also implement widthForComponent: and the width of all your components combined is less then the frame width - picker inset. I use the following:
- (void)viewDidLoad{
self.picker = [[[UIPickerView alloc] init] autorelease];
[self.view addSubview:self.picker];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.settingsPicker.frame = CGRectMake(0.0, 100.0, self.view.frame.size.width, 100.0);
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat guessedPickerInsetWidth = 24;
CGFloat pickerWidth = self.view.frame.size.width - guessedPickerInsetWidth;
if (component == SettingsPickerFirstComponent) {
return pickerWidth * 0.4; // make the first component 40%
}
return pickerWidth * 0.3; // only two others, make them 30% each
}

An iphone uipickerview that rotates horizontally?

I've only seen it in a VERY few iPhone apps... but it looks like a picker that rotates left/right (instead of top/bottom).
They usually put it on 1 line of a tableView... to allow the user to quickly pick between a small number of choices (like 3-10).
How is that coded?
Continuing the answer by Dave DeLong I got it working like this......
In viewDidLoad i did this...
CGRect frame = horizontalPickerView.frame;
frame.size.width = 50;
frame.size.height = 216;
frame.origin.x=90;
frame.origin.y = 200;
horizontalPickerView.frame = frame;
horizontalPickerView.transform = CGAffineTransformMakeRotation(3.14159/2);
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)] autorelease];
lbl.transform = CGAffineTransformMakeRotation(-3.14159/2);
lbl.text = #"hi";
return lbl;
}
Hope this helps
Here you will find source code for Picker which is horizontally aligned.
You can do this by taking a regular UIPickerView, adjusting its width (via setFrame:), and then applying an NSAffineTransform to rotate it 90º. You'll then need to rotate each item in the picker 90º the other way.
It's a little tedious to do it properly, but it can be done.
#Madhup's code lead me in the general direction I wanted when I searched for the horizontal UIPickerView but I then realized the question asked wasn't really addressed so for anyone who was looking for a more suitable answer to the left-to-right rotation. The code in the answers I'd read were all to enable left-to-right swiping, causing the picker to push the labels/rows with higher values to the left of the view. Any ways here's my contribution:
In the viewDidLoad method:
yourPickerView.frame = frame;
yourPickerView.transform = CGAffineTransformMakeRotation(4.71238898); //Instead of rotating clockwise 90° we're rotating 90° counterclockwise. 4.71238898 being ≈270° in radians.
[self.view addSubview:self.picker];
self.yourPickerView.delegate = self;
self.yourPickerView.dataSource = self;
self.yourPickerView.showsSelectionIndicator = YES;
self.yourPickerView.userInteractionEnabled = YES;
The pickerView's method:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)] autorelease];
yourLabel.transform = CGAffineTransformMakeRotation(1.57079633); //Instead of rotating counterclockwise 90° we're rotating 90° clockwise 1.57079633 being ≈90° in radians.
yourLabel.font = [UIFont fontWithName:#"System-Bold" size:18]; //Your font here.
yourLabel.text = #"yourLabel's text"; //or like me [NSString stringWithFormat:#"%#, [yourArray objectAtIndex:row]]
yourLabel.backgroundColor = [UIColor clearColor];
return label;
}
Try a paged scrollview that has the items you want on one per page, and perhaps overlay an image above it if you want nicer graphics for your control, and only allow for horizontal scrolling (don't make the contentSize of the scrollview taller than the size of the actual view, and disable vertical scroll bouncing on the control).
You will have to create picker programitcally, so that you can create your own sized picker with CGRectMake(x, y, width, height) then, you will have to rotate it, but rotating it will also rotate in the Picker's dataSources methods, you will have to rotate the view inverse of picker's rotation, I am including code hopfully it will help
.....
...
...
NSArray *arr = [NSArray arrayWithObjects:#"1 mi", #"2 mi", #"5 mi", #"10 mi", #"15 mi", #"20 mi", #"25 mi",
#"30 mi", #"35 mi", #"40 mi", #"45 mi", #"50 mi", #"75 mi", #"99 mi", nil];
radiusDefaults = [[NSMutableArray alloc] initWithArray:arr] ;
radiusPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)];
radiusPicker.delegate = self;
radiusPicker.dataSource = self;
radiusPicker.showsSelectionIndicator = NO;
//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);
rotate = CGAffineTransformScale(rotate, .1, .5);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(-61, 0);
radiusPicker.transform = CGAffineTransformConcat(rotate,t0);
// [theNavigationBar.topItem setTitleView:radiusPicker] ;
UIView *pickerWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 215)];
[self.view addSubview:radiusPicker];
[radiusPicker selectRow:6 inComponent:0 animated:NO];
[radiusPicker release];
.....
.......
....
#pragma mark -
#pragma mark UIPickerView
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 400)] autorelease];
UILabel *label;
UIFont *font = [ UIFont fontWithName:#"ArialRoundedMTBold" size:22];
label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 70, 350)] autorelease];
[label setText:[NSString stringWithFormat:#"%#", [radiusDefaults objectAtIndex:row]]];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blueColor];
label.font = font;
label.backgroundColor = [UIColor clearColor];
// label.opaque = NO;
[viewForRow addSubview:label];
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);
rotate = CGAffineTransformScale(rotate, 1, 6.5);
[viewForRow setTransform:rotate];
return viewForRow;
}
Try this->
Create Plain UIVew of size of UIPickerview, add picker on it.
set numberOfComponentsInPickerView = 1.
set componant width.
Then add small sub views on it to Hide rest of picker. Only rotating wheel of componant should visible.
Transform plain view to rotate it through 90 degree.
Make sure to apply tranform in:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = nil;
if(view)
{
lbl = (UILabel *) [view viewWithTag:11];
}
else
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 0, 30, 30)];
lbl.tag = 11;
lbl.backgroundColor = [UIColor clearColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
lbl.transform = CGAffineTransformRotate(lbl.transform, M_PI +M_PI/2);
[view addSubview:lbl];
[lbl release];
}
lbl.text = [dataSourceArray objectAtIndex:row];
return view;
}
Now you can add palin view as a subview for horizontal picker on any view.
Have you ever considered taking a table view and rotating it?
Didn't think so. Go try that. :)

right align text in UIPickerView

How can I right align text in a UIPickerView? I tried to make custom UILabels for the row views, but for some reason, nothing is showing up, right-aligned or otherwise. Here's what I wrote:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setText:[NSString stringWithFormat:#"row %d", row]];
[label setTextAlignment:UITextAlignmentRight];
return [label autorelease];
}
In case anyone is wondering, I used CGRectZero because I saw it in the UICatalog example.
I have taken two components in picker and two arrays to set the title of the rows in a particular component.
Below code will display pickerdata in center with default font and fontsize of the picker.
It will give exact pickerdata display behavior with center alignment of the pickerdata.
Here,
NSArray *component1Array=[NSArray arrayWithObjects:#"0 lbs",#"1 lbs",#"2 lbs",#"3 lbs",#"4 lbs",#"5 lbs",nil];
NSArray *component2Array=[NSArray arrayWithObjects:#"0.00 oz",#"0.25 oz",#"0.50 oz",#"0.75 oz",#"1.00 oz",nil];
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//I have taken two components thats why I have set frame of my "label" accordingly. you can set the frame of the label depends on number of components you have...
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)];
//For right alignment of text,You can set the UITextAlignmentRight of the label.
//No need to set alignment to UITextAlignmentLeft because it is defaulted to picker data display behavior.
[label setTextAlignment:UITextAlignmentCenter];
label.opaque=NO;
label.backgroundColor=[UIColor clearColor];
label.textColor = [UIColor blackColor];
UIFont *font = [UIFont boldSystemFontOfSize:20];
label.font = font;
if(component == 0)
{
[label setText:[NSString stringWithFormat:#"%#",[component1Array objectAtIndex:row]]];
}
else if(component == 1)
{
[label setText:[NSString stringWithFormat:#"%#", [component2Array objectAtIndex:row]]];
}
return [label autorelease];
}
You should comment below mention UIPickerView delegate method if you are using above method...
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
The output of above sample code will look like below
Your not seeing anything because of CGRectZero. You need to set a size in your case.
In the UICatalog, if your talking about how they used CGRectZero for the CustomView... well if you look at CustomView.m you will see they are actually disregarding the CGRectZero and setting a frame to a size in the initWithFrame:
In iOS 6, you can now return an NSAttributedString, which can contain text alignment attributes. I posted a short snippet on another related question here: https://stackoverflow.com/a/14035356/928963
Make sure you:
Provide an explicit frame that is as high and wide as needed;
Set the label's display opaqueness (via .opaque = YES or NO) and background color appropriately (you usually want NO and [UIColor clearColor] respectively).
Set the font explicitly.

UIPicker sizing in landscape mode

I am trying to develop an app with a UIPicker in landscape mode, taking up (almost) the entire width of the screen (with 5 or 6 components). Can you please tell me how to set the size of UIPicker. Thank you very much for your help.
Actually, I resize my pickers for almost every app. I do not like that they take up the entire screen. Here is the method that I am using: (note that I am also rotating the picker to be horizontal)
in viewDidLoad .....
picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = NO;
//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);
rotate = CGAffineTransformScale(rotate, .46, 2.25);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(3, 22.5);
picker.transform = CGAffineTransformConcat(rotate,t0);
[self.view addSubview:picker];
[picker release];
Then, I like to add a background image for my view that covers up the grey bars (which are now on the top and bottom) of the UIPicker:
//Create the overlay to superimpose ontop of the picker
//In this case, the image is the size of the screen with a transparent area the size of the //UIPickerView cut out in the middle
UIImageView *uiiv = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"problem_bg.png"]];
uiiv.userInteractionEnabled = NO;
uiiv.opaque = YES; //for performance
[self.view addSubview:uiiv];
[uiiv release];
UIPickerView delegate method:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)rowforComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 280)] autorelease];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage.png"]];
img.frame = CGRectMake(0, 0, 102,280);
img.opaque = YES;
[viewForRow addSubview:img];
[img release];
UILabel *label;
UIFont *font = [ UIFont fontWithName:#"Helvetica" size:20];
label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 270, 100)] autorelease];
label.text = #"I am a label";
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.font = font;
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[viewForRow addSubview:label];
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);
[viewForRow setTransform:rotate];
return viewForRow;
}
This gives a much smaller, horizontal picker with a nice look and feel. I hope this helps someone.
You can edit the size by opening the .xib file in TextEdit and changing the size of the UIPickerView.
You can't. UIPickerView's size is constant.
UPDATE: It turns out you can resize an UIPickerView. The trick is to place it inside another (smaller) UIView, and resize that view. I haven't tried this yet.
UPDATE 2: This method does not resize the UIPickerView, but rather crops it. It might or might not be what you're looking for, but AFAIK, there's no way to truly resize an UIPickerView, and this is as close as it gets. It doesn't look that bad.
UPDATE 3 (Long overdue): As of SDK 3.0, UIPickerView is completely resizeable using initWithFrame or setFrame.
I wrestled with the same issue of resizing the pickerview. After some research and headache here is something you can easily do:
Forget Interface builder! I think you can so much better UI without
the interface builder.
In your controllers overwrite the load view method.
-(void)loadView
{
//create your view
self.view = [[UIView alloc] initWithFrame:CGRectZero];
// create a default sized pickerView
pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
// Get its frame
CGRect pickerFrame = pickerView.frame;
// Set it to what ever you like, I use a default screen height times a shrink factor like 0.75 for 3/4 of its original size
pickerFrame.size.width = screenWidth*pickerShrinkFactor;
pickerFrame.size.height = pickerHeight*pickerShrinkFactor;
// You can also set the upper left corner of the picker
pickerFrame.origin.x = 0;
// Set the picker frame to new size and origin
pickerView.frame = pickerFrame;
// Add it to your view
[self.view addSubview:pickerView];
}
Good luck
I'm fairly certain that the UIPicker comes in one size, which you can't change. Would be interested to hear different.
The answer is in:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
Since with:
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
Makes it work (that is: sideways with scaled label) of course
No problem (I just registered). By the way I just realized that when I posted my answer, that the method (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent .... lost the space between row and forComponent. So make sure that you have the delegate correct or else it will not work.