2013-08-26 99 views
1

我看到一個this question and answer,我嘗試了幾個選項,但沒有工作。在UIPickerView中添加固定的UILabel

我想創建類似下面的一個UIPickerView,(固定標籤英寸和英尺),但這些不會出現:

enter image description here 我創建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]; 
} 

選取器出現,但沒有固定的英寸和英尺標籤。

出了什麼問題?

回答

0

我用了已經實現的巨大分量線:

https://github.com/brunow/ActionSheetPicker2

它提供了一個多列選擇器視圖,我根本改了文字和列數

+0

使用'ActionSheetPicker-3.0','〜> 1.3.10'http://skywinder.github.io/ActionSheetPicker-3.0 – Ted

+0

也許 - 我寫了這個答案1.5年前:)但謝謝 – Dejell

0
  1. 移動此行viewDidLoad並嘗試it.Labels需要添加 once.Not總是在文本框並開始編輯

    [self addPickerLabel:@"Feet" rightX:114 top:342 height:21]; 
    [self addPickerLabel:@"Inches" rightX:241 top:342 height:21]; 
    
  2. 登錄這兩個標籤的幀,將其正確如果出現錯誤

    NSLog(@"%@",NSStringFromCGRect(label.frame)); 
    
+0

我試過了。我仍然看不到它們 – Dejell

+0

查看2並查看結果 –

+0

{{183,343},{241,21}} {{183,342},{241,21}} – Dejell

0

你UIPickerView具有喜ght是216,但是你把標籤放在高度342.這可能是你看不到它們的原因。

編輯: 嘗試更換,你做的標籤

[self addPickerLabel:@"Feet" rightX:114 top:98 height:21]; 
    [self addPickerLabel:@"Inches" rightX:241 top:98 height:21]; 
+0

我的身高是21! – Dejell

+0

CGRect pickerFrame = CGRectMake(0,0,200,216);很明顯,pickerView的框架是216.你的標籤超出了他們的superView的界限。 – johnyu

+0

我試過你的編輯代碼[self addPickerLabel:@「Feet」rightX:114 top:98 height:21]; [self addPickerLabel:@「Inches」rightX:241 top:98 height:21];但它不會幫助 – Dejell