Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Change / Customize Picker Size

How does one change the picker size to say shorten the width?

Posted on Mar 22, 2009 10:44 AM

Reply
8 replies

Mar 22, 2009 5:57 PM in response to dancinglipstickpig

Assuming you mean the overall frame size of UIPickerView, I was fairly sure it was locked but got curious when I saw your question and found this in another forum: [http://stackoverflow.com/questions/573979/how-to-change-uipickerview-height]. From the description of what happens when you change either dimension though, that capability sounds more like a bug than a feature.

There are delegate methods to set the row height and width of each component in case that's what you meant.

Mar 22, 2009 9:31 PM in response to dancinglipstickpig

No, the UIPickerViewDelegate Protocol doesn't include any methods that affect the size of the frame. The picker in the app you mentioned might have been created in code per the link I gave you, or it might have been made from a custom class. It might be worth the time to experiment with different sizes as suggested in that other thread. Maybe you can find a size that will work for you without any bugginess.

Mar 23, 2009 12:32 AM in response to RayNewbie

Thanks for your help. I'm trying to experiment adding the code you should me of:

smallerPicker [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 120.0)

where exactly do I put this and do I need to change anything else?

Here is my implementation file coding:
//
// SingleComponentPickerViewController.m
// Pickers
//

#import "SingleComponentPickerViewController.h"


@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;

- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
- (IBAction)buttonPressed
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
@"You selected %@!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:@"Thank you for choosing."
delegate:nil
cancelButtonTitle:@"You're Welcome"
otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia",
@"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
self.pickerData = array;
[array release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceO rientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}


- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickervView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
@end




and here is my header file coding:

//
// SingleComponentPickerViewController.h
// Pickers
//
// Created by Sean on 3/22/09.
// Copyright 2009 Murphy Electric. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface SingleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
@property (nonatomic, retain) UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed;
@end

Mar 23, 2009 2:27 AM in response to dancinglipstickpig

I went ahead and created a picker in code to see if I could change the frame size as claimed at that link (easy since I too built the Pickers project from Mark & LaMarche--great book!).

No matter what frame size I tried, the numbers were ignored and the picker came out the same size as always. Unless there's an implied trick I didn't understand in that post, I'd say it's bogus, and I'm sorry I pointed you there without testing it first.

If you still want to try for yourself, just delete the picker from the view in IB and add the commented code to SingleComponentPickerViewController.m:

- (void)viewDidLoad {
[super viewDidLoad];

// make the picker here instead of in IB
UIPickerView *smallerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 216.0)];
smallerPicker.dataSource = self;
smallerPicker.delegate = self;
smallerPicker.showsSelectionIndicator = YES;
[self.view addSubview:smallerPicker];
self.singlePicker = smallerPicker;
[smallerPicker release];

NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia",
@"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
self.pickerData = array;
[array release];
}

I guess all is not lost, since it can be just as instructive to check out bad info as good info. And every time I build something in code that I'm used to getting with one or two clicks in IB, I realize what a powerful tool it is.

Mar 23, 2009 2:47 AM in response to dancinglipstickpig

Yeah, that's how to change the frame width of an object that already exists, and that would be the normal thing to do if we assumed the frame could be changed after it had been initialized. But the claim we wanted to test was that the otherwise immutable UIPickerView frame dimensions could be set to something other than the defaults by initWithFrame.

Change / Customize Picker Size

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.