IOS,如何将自定义SelectionIndicator添加到UIPickerView?

时间:2011-09-28 06:26:27

标签: iphone objective-c uipickerview

我想为UIPickerView添加一个复选标记而不是默认的SelectionIndicator。

enter image description here

我正在尝试将图片添加为子视图

 UIImageView * checkMark = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 102, 13, 13)] autorelease];
[checkMark setImage:[UIImage imageNamed:@"pickerChechmark.png"]];
checkMark.layer.borderColor = [[UIColor redColor] CGColor];
checkMark.layer.borderWidth = 2;
 [picker addSubview:checkMark];

但它被添加到选择器后面,请看下图。

for x =0 for x =5 enter image description here

图像1表示x = 0,图像2表示x = 5,图像3表示x = 10 (x复选标记的位置) 还可以看到左上方的红色矩形,以澄清我的问题

UIView * redblock = [[[UIView alloc] initWithFrame:CGRectMake(0, -20, 50, 40)] autorelease];
redblock.backgroundColor = [UIColor redColor];
[picker addSubview:redblock];
[picker bringSubviewToFront:redblock];

如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

我还需要自定义选择指标,因为我使用高于标准指标的自定义视图(它们类似于表视图单元格)填充选择器。

我最初尝试指定在创建时叠加到UIPickerView的半透明视图会导致相同的结果。在跟踪时,我注意到选择器视图在创建时还没有子视图,这可能是自定义选择指示器被发送到可视层次结构背面的原因。

我的解决方案包括在第一次调用委托方法 pickerView:viewForRow:forComponent:reusingView:时指定选择指示器的自定义视图。在那个时间点,UIPickerView准备好了所有的子视图,我的新自定义视图被正确地附加在其余视图之上。如果您不需要对内容使用自定义视图,则可以使用其他数据源方法 pickerView:titleForRow:forComponent:

以下代码段显示了我的解决方案。

注意:如果委托方法提供多个UIPickerView,则需要以不同方式处理该标志; SLPickerCell是我自己的单元类,只是一个UIView子类。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { SLPickerCell *pickerCell = nil; NSArray *topLevelObjects; const CGFloat kPickerBorderSize = 10; const CGFloat kIndicatorVerticalMargin = 2; // if this is the first time we enter the function, add selection indicator view on top of everything else if ( ![self pickerSelectionIndicatorInstalled] ) { UIView *customSelectionIndicator; CGRect selectionIndicatorFrame; CGRect pickerViewFrame; pickerViewFrame = [pickerView frame]; // Create and add a custom selection indicator (a superimposed semi-transparent view) // Use the cell height as reference for the height of the indicator selectionIndicatorFrame = CGRectMake(kPickerBorderSize, (pickerViewFrame.size.height - ( [SLPickerCell height] + kIndicatorVerticalMargin )) / 2, pickerViewFrame.size.width - (2 * kPickerBorderSize), ( [SLPickerCell height] + kIndicatorVerticalMargin )); customSelectionIndicator = [[UIView alloc] initWithFrame:selectionIndicatorFrame]; [customSelectionIndicator setUserInteractionEnabled:NO]; [customSelectionIndicator setAlpha:0.25]; [customSelectionIndicator setBackgroundColor:[UIColor lightGrayColor]]; [[customSelectionIndicator layer]setBorderWidth:1.0]; [[customSelectionIndicator layer]setBorderColor:[[UIColor darkGrayColor]CGColor]]; // Add the indicator on top of the picker [pickerView addSubview:customSelectionIndicator]; // set the flag; remember to reset it upon creation of a new UIPickerView [self setPickerSelectionIndicatorInstalled:YES]; } // [...]

答案 1 :(得分:1)

试试这段代码,我在我的选择器视图中添加了一个复选标记和标签

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)theView{

    UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero];

    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(-105, -15, 240, 30)];
    [lbl setBackgroundColor:[UIColor clearColor]];
    [lbl setText:[self.folderNameArray objectAtIndex:row]];
    [lbl setFont:[UIFont boldSystemFontOfSize:20]];
    [pickerviewtemp addSubview:lbl];
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(-130, -5, 15, 15)];
if ([self.folderCheckArray containsObject:[NSString stringWithFormat:@"%d",row]]) {
        [imgView setImage:[UIImage imageNamed:@"tick.png"]];
    }
    else {      
        [imgView setImage:nil];
    }   

    [pickerviewtemp addSubview:imgView];
    return pickerviewtemp;
}

答案 2 :(得分:0)

尝试添加复选标记以便像这样查看:

[self.view addSubview:checkMark];

你将不得不摆弄坐标,但你应该能够在选择器视图中看到它。