何将自定义SelectionIndicator添加到UIPick

何将自定义SelectionIndicator添加到UIPick

本文介绍了IOS,如何将自定义SelectionIndicator添加到UIPickerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add a checkmark instead of the default SelectionIndicator for UIPickerView.

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

I am trying to add an image as subview

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

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

but its been added behind the picker see the following image.



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

image 1 for x =0, image 2 for x =5, image 3 for x = 10(x position of checkmark)also see the red rectangle on top left, for clarification on my question

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

如何解决这个问题?

推荐答案

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

I also needed to customize the selection indicator because I populate the picker with custom views (they are similar to table view cells) that are higher than the standard indicator.

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

My initial attempt to specify a semi-transparent view to be superimposed to the UIPickerView upon creation led to the same result. While tracing I noticed that the picker view doesn't have subviews yet at the time of creation, which is probably the reason for the custom selection indicator be sent to the back of the visual hierarchy.

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

My solution consists of specifying the custom view for the selection indicator the first time the delegate method pickerView:viewForRow:forComponent:reusingView: is called. At that point in time the UIPickerView is ready with all its subviews and my new custom view is properly appended on top of the rest. If you don't need to use custom views for the contents, you could do the same using the other data source method pickerView:titleForRow:forComponent:.

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

The following snippet shows my solution.

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

Notes: if the delegate method serves more than one UIPickerView you need to handle the flag differently; SLPickerCell is my own cell class, just a UIView subclass.

这篇关于IOS,如何将自定义SelectionIndicator添加到UIPickerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:58