繁体   English   中英

如何更改UIPickerView中每个组件的宽度?

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

如何更改UIPickerView中每个组件的宽度?

pickerView:widthForComponent:的委托中实现pickerView:widthForComponent:方法,并从中返回每个组件的适当宽度。 例如

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
      switch (component){
           case 0: 
                return 100.0f;
           case 1: 
                return 60.0f;
     }
      return 0;
}

Swift 4版本:

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
}

在此示例中:第一个组件的宽度为2/3,第二个组件的宽度为1/3

由于各种屏幕尺寸,使用固定值可能是个坏主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM