Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
Peter Warbo Avatar asked Dec 02 '25 05:12

Peter Warbo


2 Answers

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

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.

like image 35
null0pointer Avatar answered Dec 03 '25 19:12

null0pointer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!