1

I saw a this question and answer and I tried a few options but non worked.

I would like to create a UIPickerView like the one below, (fixed labels inches and feet) but those wouldn't appear:

enter image description here I create the UIImagePicker like this:

- (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];
}

The picker appears, but without the fixed labels of inches and feet.

What is wrong?

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229

3 Answers3

0
  1. Move this lines to viewDidLoad and try it.Labels need to be added once.Not always when textfield did begin editing

    [self addPickerLabel:@"Feet" rightX:114 top:342 height:21];
    [self addPickerLabel:@"Inches" rightX:241 top:342 height:21];
    
  2. Log the frame of both the label and set it correct if it appears wrong

    NSLog(@"%@",NSStringFromCGRect(label.frame));
    
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

Your UIPickerView has height of 216, but you put the labels at height 342. This might be the reason you can't see them.

Edit: Try replacing the lines where you make the labels to

    [self addPickerLabel:@"Feet" rightX:114 top:98 height:21];
    [self addPickerLabel:@"Inches" rightX:241 top:98 height:21];
johnyu
  • 2,152
  • 1
  • 15
  • 33
  • CGRect pickerFrame = CGRectMake(0, 0, 200, 216); Clearly frame of pickerView is 216. Your labels are simply out of bounds of their superView. – johnyu Aug 26 '13 at 10:45
  • I tried your edited code [self addPickerLabel:@"Feet" rightX:114 top:98 height:21]; [self addPickerLabel:@"Inches" rightX:241 top:98 height:21]; but it wouldn't help – Dejell Aug 26 '13 at 10:48
  • But did you move them to viewDidLoad as @Lithu T.V suggested? – johnyu Aug 26 '13 at 10:50
  • Yes. I did move them to viewDidLoad – Dejell Aug 26 '13 at 10:50
  • Does it work if you remove the [pickerView removeFromSuperview]; line from viewDidLoad? Also, I see you have _picker (a property) and also use pickerView. Is this a typo or are they two separate objects? – johnyu Aug 26 '13 at 10:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36248/discussion-between-johnyu-and-odelya) – johnyu Aug 26 '13 at 11:16
  • I decided to use https://github.com/brunow/ActionSheetPicker2 which has a the fixed label implemented already. Thanks for the help! – Dejell Aug 26 '13 at 14:10
0

I used the already implemented great component:

https://github.com/brunow/ActionSheetPicker2

Which provides a multicolumn picker view and I simply changed the text and the amount of columns

Dejell
  • 13,947
  • 40
  • 146
  • 229