mamrezo
mamrezo

Reputation: 2285

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

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

Upvotes: 6

Views: 11469

Answers (2)

Makalele
Makalele

Reputation: 7521

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.

Upvotes: 4

Vladimir
Vladimir

Reputation: 170829

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;
}

Upvotes: 35

Related Questions