Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the width of each component in a UIPickerView?

How can I change the width of each component in a UIPickerView?

like image 476
mamrezo Avatar asked Dec 02 '10 17:12

mamrezo


2 Answers

Implement pickerView:widthForComponent: method in picker's delegate and return appropriate width for each component from it. e.g.

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
      switch (component){
           case 0: 
                return 100.0f;
           case 1: 
                return 60.0f;
     }
      return 0;
}
like image 190
Vladimir Avatar answered Nov 08 '22 21:11

Vladimir


Swift 4 version:

func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    let w = pickerView.frame.size.width
    return component == 0 ? (2 / 3.0) * w : (1 / 3.0) * w
}

in this example: 2/3 of width for first component and 1/3 for the second

Using fixed values may be a bad idea, because of various screen sizes.

like image 32
Makalele Avatar answered Nov 08 '22 19:11

Makalele