1

我看到了这个问题和答案,我尝试了一些选项,但没有奏效。

我想创建一个像下面这样的 UIPickerView (固定标签英寸和英尺),但那些不会出现:

在此处输入图像描述 我像这样创建 UIImagePicker:

- (void)viewDidLoad
{
   _picker = [[UIPickerView alloc] init];
   CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
    pickerView.frame = pickerFrame;
    pickerView.userInteractionEnabled = YES;
    pickerView.dataSource = self;
    pickerView.hidden = YES;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    [textField setInputView:pickerView];
    textField.delegate = self;

    [pickerView removeFromSuperview];
   _picker.hidden = YES;
}


- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
    if (textView.tag==1){ //field for the uipickerview
        _picker.hidden = NO;
        [self addPickerLabel:@"Feet" rightX:114 top:342 height:21];
        [self addPickerLabel:@"Inches" rightX:241 top:342 height:21];
    }
    return YES;
}

- (void)addPickerLabel:(NSString *)labelString rightX:(CGFloat)rightX top:(CGFloat)top height:(CGFloat)height {
#define PICKER_LABEL_FONT_SIZE 18
#define PICKER_LABEL_ALPHA 0.7
    UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
    CGFloat x = rightX - [labelString sizeWithFont:font].width;

    // White label 1 pixel below, to simulate embossing.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, top + 1, rightX, height)];
    label.text = labelString;
    label.font = font;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
    label.alpha = PICKER_LABEL_ALPHA;
    [_picker addSubview:label];


    // Actual label.
    label = [[UILabel alloc] initWithFrame:CGRectMake(x, top, rightX, height)];
    label.text = labelString;
    label.font = font;
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
    label.alpha = PICKER_LABEL_ALPHA;
    [_picker addSubview:label];
}

选择器出现,但没有英寸和英尺的固定标签。

怎么了?

4

3 回答 3

0
  1. 将此行移至viewDidLoad并尝试。标签需要添加一次。并非总是在文本字段开始编辑时

    [self addPickerLabel:@"Feet" rightX:114 top:342 height:21];
    [self addPickerLabel:@"Inches" rightX:241 top:342 height:21];
    
  2. 记录两个标签的框架并在出现错误时将其设置正确

    NSLog(@"%@",NSStringFromCGRect(label.frame));
    
于 2013-08-26T10:29:58.677 回答
0

您的 UIPickerView 的高度为 216,但您将标签放置在高度 342。这可能是您看不到它们的原因。

编辑:尝试替换您制作标签的行

    [self addPickerLabel:@"Feet" rightX:114 top:98 height:21];
    [self addPickerLabel:@"Inches" rightX:241 top:98 height:21];
于 2013-08-26T10:35:55.337 回答
0

我使用了已经实现的很棒的组件:

https://github.com/brunow/ActionSheetPicker2

它提供了一个多列选择器视图,我只是更改了文本和列数

于 2013-08-26T14:10:25.080 回答