iOS 上每个组件的 UIPickerView 固定标签问题 6

Issue with UIPickerView fixed label for each component on iOS 6

我想显示 UIPickerView 每个组件都有一些硬编码的文本。类似于 "Hours" 和 "Mins"。现在,我可以通过创建标签并将其作为子视图放入选择器视图来实现这一点。这适用于 iOS 7 及更高版本。

但对于 iOS 6,我的标签被 UIPickerView 组件重叠,因此它们不可见。我怎样才能为 iOS 6 实现此视图,其中对于每个组件,值都被标记为固定标签,以便用户知道他们正在选择什么。

我认为您可以通过构造一个包含选择器视图和标签的自定义视图来实现此目的。

为了他人的利益,我是这样实现的(基于Frog的建议):

- (void)setDatePickerView:(MyCustomCell *)iCell {
    UIView *inputView = [[UIView alloc] init];

    // Add Picker First
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.tag = 100;

    int height = 30;
    int offsetY = pickerView.frame.size.height / 2 - height / 2;

    // Add a line on top of selection
    if ([Utilities isIOS7orAbove]) {
        UIImageView *topLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, offsetY - 2, pickerView.frame.size.width, 1)];
        [topLineView setBackgroundColor:[UIColor grayColor]];
        [pickerView addSubview:topLineView];
    }

    [inputView addSubview:pickerView];

    // Add Minute Label
    CGFloat minLabelX = [Utilities isIOS7orAbove] ? 97 : 90;
    CGFloat labelYM = [Utilities isIOS7orAbove] ? 0.0 : 1.0;
    CGFloat labelW = 50;

    UILabel *minutesLabel = [[UILabel alloc] initWithFrame:CGRectMake(minLabelX, offsetY - labelYM, labelW, height)];
    minutesLabel.text = @“Mins";
    minutesLabel.font = [UIFont systemFontOfSize:kFontSize22];
    minutesLabel.backgroundColor = [UIColor clearColor];
    minutesLabel.textColor = [UIColor blackColor];
    [inputView addSubview:minutesLabel];

    // Now Add Seconds Label
    CGFloat secLabelX = [Utilities isIOS7orAbove] ? 230 : 218;
    UILabel *secondsLabel = [[UILabel alloc] initWithFrame:CGRectMake(secLabelX, offsetY - labelYM, labelW, height)];
    secondsLabel.text = @“Secs";
    secondsLabel.font = [UIFont systemFontOfSize:kFontSize22];
    secondsLabel.backgroundColor = [UIColor clearColor];
    secondsLabel.textColor = [UIColor blackColor];
    [inputView addSubview:secondsLabel];

    iCell.inputField.inputView = inputView;
}

只需使用ActionSheetPicker3.0,我在需要自定义选择器时一直使用它。它与 iOS6+.

完美配合

这是您将它用于字符串选择器的方式:

NSArray *colors = [NSArray arrayWithObjects:@"Hours", @"Mins", @"Whatever", @"More", nil];

[ActionSheetStringPicker showPickerWithTitle:@"Select a Color"
                                        rows:colors
                            initialSelection:0
                                   doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
                                      NSLog(@"Picker: %@, Index: %@, value: %@", 
                                      picker, selectedIndex, selectedValue);
                                    }
                                 cancelBlock:^(ActionSheetStringPicker *picker) {
                                      NSLog(@"Block Picker Canceled");
                                    }
                                      origin:sender];

尽情享受吧!