RickiG
RickiG

Reputation: 11390

UIPickerView takes up all horizontal space on iPhone, but not on iPad?

I noticed something that has never been a problem before. I did a project for iPad where I used several UIPickerView positioned next to each other, horizontally. Here they respect the CGRect frame I initialize them with, meaning placing other elements on either side of them was no problem.

Now I am trying to do this on an iPhone project and here a UIPickerView insists on being the only element. It sizes it self to fill the screen horizontally, with the "around" graphics.

I tried different approaches, place the UIPickerView inside a different view then sizing that super view, that just leads to clipping, not resizing. Another thing is that the UIPicker insists on being placed in the center of the screen. This basically means that when a UIPickerView is added to the screen, even though its single component is only 70 px wide, those 320 px of the screen is used up.

What I am trying to accomplish is to have a UIPicker on the right side of the screen and a button to the left of it.

Am I overlooking something obvious here? Hope someone could lend a hand, thanks in advance:)

Nothing more complicated than this:

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, 165.0f)];

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:container.frame];
[picker setDelegate:self];
[picker setDataSource:self];
[container addSubview:picker];

the frame I set, is not respected. It takes up all horizontal space.

Upvotes: 0

Views: 1016

Answers (1)

TomSwift
TomSwift

Reputation: 39512

I tried your code with the same result.

However, you can set the frame after the picker has been created and added to your container, and the new size is respected. Here's my test case, which works for me using SDK 4.2, in the iPhone simulator:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPickerView* pv = [[[UIPickerView alloc] initWithFrame: CGRectMake(160, 100, 100, 216) ] autorelease];
    pv.delegate = self;
    pv.dataSource = self;
    [self.view addSubview: pv];

    pv.frame = CGRectMake(10, 10, 100, 216);
}

Upvotes: 1

Related Questions