How can I change the width of each component in a UIPickerView
?
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With