Peter Warbo
Peter Warbo

Reputation: 11710

iOS - Change UIPickerView component width

I know I can set the UIPickerView´s component width with the delegate method – pickerView:widthForComponent: but say if I have 2 components and I only want to change the first component's width and leave the second component's width to the default value (something that iOS SDK would figure out). How would I achieve this?

Upvotes: 2

Views: 9403

Answers (3)

Malfunction
Malfunction

Reputation: 1334

This is a late answer, but for future readers, apparently there is no way to return a "default" width. The moment you define the pickerView:widthForComponent: method, you'll have to explicitly set all the components' widths, not just some of them.

Upvotes: 3

user3100146
user3100146

Reputation:

PickerView divide your component on the basis of how much component there are in picker. so there are no default value for component . but you can still try this .

For that , try to set your component width by percentage style .

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2 ;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return 10 ;
    }
    else
    {
        return 10 ;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return @"Apple" ;
    }
    else
    {
        return @"iPhone" ;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0)
    {
        return (self.view.frame.size.width * 55 ) / 100  ;
    }
    else
    {
        return (self.view.frame.size.width * 30 ) / 100  ;
    }   
}

Here is screenShot :

enter image description here

Upvotes: 9

null0pointer
null0pointer

Reputation: 1543

Unless you use an if-statement and manually set the default value (after finding out what it is) like this:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0)
        return customWidth;
    return defaultValue;
}

I'm not sure you can set the value for only some of the components but let the default take-over for others.

Upvotes: 5

Related Questions