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

时间:2016-04-08 03:43:10

标签: ios objective-c uipickerview

我想显示一个UIPickerView,其中包含一些针对每个组件硬编码的文本。像“小时”和“分钟”之类的东西。现在,我可以通过创建标签作为子视图并将其放入选择器视图来实现此目的。这适用于iOS 7&上方。

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

为了别人的利益,这就是我实现这个目标的方式(根据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;
}

答案 2 :(得分:0)

只需使用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];

享受!